Skip to main content
added 792 characters in body
Source Link
Zibelas
  • 5k
  • 2
  • 15
  • 26
[Serializable]
public class BlendKey {
    [SerializeField]
    public string soundName;
    [SerializeField]
    public List<String>List<NeemData> neemList;

    public override bool Equals(object obj) {
        if (obj == null)
            return false;
        BlendKey c = obj as BlendKey ;
        if (c == null)
  return false;
        if (!soundName.Equals(c.soundName)) return false;
        if (neemList.Count != c.neemList.Count) return soundNamefalse;
        for (int index = 0; index < neemList.Count; index++) {
            if (!neemList[index].neemName.Equals(c.soundNameneemList[index].neemName);) return false;
        }

        return true;
    }

    public override int GetHashCode() {
        int listHash = 0;
        for (int index = 0; index < neemList.Count; index++) {
            listHash += neemList[index].neemName.GetHashCode();
        }
        return soundName.GetHashCode(); + listHash;
    }
}


public class TestDic: MonoBehaviour {
    
    public Dictionary<BlendKey, string> testDic = new Dictionary<BlendKey, string>();        
    public NeemData[] NeemDatas;

    private void Start() {
        BlendKey testKey = new BlendKey();
        testKey.soundName = "ABC";
        testKey.neemList = new List<string>List<NeemData>{"1"NeemDatas[0], "2"NeemDatas[1]};
        testDic.Add(testKey, "Sound 1");
        
        BlendKey testKey2 = new BlendKey();
        testKey2.soundName = "XYZ";
        testKey2.neemList = new List<string>List<NeemData>{"8"NeemDatas[1], "9"NeemDatas[2]};
        testDic.Add(testKey2, "Sound 2");
    
        BlendKey testKey3 = new BlendKey();
        testKey3.soundName = "ABC";
        testKey3.neemList = new List<string>List<NeemData>{"1"NeemDatas[1], "2"NeemDatas[0]};
        
        Debug.Log(testDic[testKey]);
        Debug.Log(testDic[testKey2]);
        Debug.Log(testDic[testKey3]);
    }
}

The third testDic[testKey3]); will give you a key not found error since it is not the same data as from testKey (it has the same NeemData, but the order is different). You could solve this by further adding everything for a sort interface but if that is really needed will depends on if the NeemData is generated dynamically or by hand.

[Serializable]
public class BlendKey {
    [SerializeField]
    public string soundName;
    [SerializeField]
    public List<String> neemList;

    public override bool Equals(object obj) {
        if (obj == null)
            return false;
        BlendKey c = obj as BlendKey ;
        if (c == null)
             return false;
        return soundName.Equals(c.soundName);
   }

    public override int GetHashCode() {
        return soundName.GetHashCode();
    }
}


public class TestDic: MonoBehaviour {
    
    public Dictionary<BlendKey, string> testDic = new Dictionary<BlendKey, string>();
    
    private void Start() {
        BlendKey testKey = new BlendKey();
        testKey.soundName = "ABC";
        testKey.neemList = new List<string>{"1", "2"};
        testDic.Add(testKey, "Sound 1");
        
        BlendKey testKey2 = new BlendKey();
        testKey2.soundName = "XYZ";
        testKey2.neemList = new List<string>{"8", "9"};
        testDic.Add(testKey2, "Sound 2");

        BlendKey testKey3 = new BlendKey();
        testKey3.soundName = "ABC";
        testKey3.neemList = new List<string>{"1", "2"};
        
        Debug.Log(testDic[testKey]);
        Debug.Log(testDic[testKey2]);
        Debug.Log(testDic[testKey3]);
    }
}
[Serializable]
public class BlendKey {
    [SerializeField]
    public string soundName;
    [SerializeField]
    public List<NeemData> neemList;

    public override bool Equals(object obj) {
        if (obj == null) return false;
        BlendKey c = obj as BlendKey ;
        if (c == null) return false;
        if (!soundName.Equals(c.soundName)) return false;
        if (neemList.Count != c.neemList.Count) return false;
        for (int index = 0; index < neemList.Count; index++) {
            if (!neemList[index].neemName.Equals(c.neemList[index].neemName)) return false;
        }

        return true;
    }

    public override int GetHashCode() {
        int listHash = 0;
        for (int index = 0; index < neemList.Count; index++) {
            listHash += neemList[index].neemName.GetHashCode();
        }
        return soundName.GetHashCode() + listHash;
    }
}


public class TestDic: MonoBehaviour {
    
    public Dictionary<BlendKey, string> testDic = new Dictionary<BlendKey, string>();        
    public NeemData[] NeemDatas;

    private void Start() {
        BlendKey testKey = new BlendKey();
        testKey.soundName = "ABC";
        testKey.neemList = new List<NeemData>{NeemDatas[0], NeemDatas[1]};
        testDic.Add(testKey, "Sound 1");
    
        BlendKey testKey2 = new BlendKey();
        testKey2.soundName = "XYZ";
        testKey2.neemList = new List<NeemData>{NeemDatas[1], NeemDatas[2]};
        testDic.Add(testKey2, "Sound 2");
    
        BlendKey testKey3 = new BlendKey();
        testKey3.soundName = "ABC";
        testKey3.neemList = new List<NeemData>{NeemDatas[1], NeemDatas[0]};

        Debug.Log(testDic[testKey]);
        Debug.Log(testDic[testKey2]);
        Debug.Log(testDic[testKey3]);
    }
}

The third testDic[testKey3]); will give you a key not found error since it is not the same data as from testKey (it has the same NeemData, but the order is different). You could solve this by further adding everything for a sort interface but if that is really needed will depends on if the NeemData is generated dynamically or by hand.

deleted 15 characters in body
Source Link
Zibelas
  • 5k
  • 2
  • 15
  • 26

This can be easily solved by your first bullet point. Just implement the override. You will need to come up with a way to identify NeemData List on your own since I do not know the structure of it. It might be as easy as just taking the length of the array (might not be 100% unique but that would mean you first need to have the same soundname with the same amount of sound clips).

[Serializable]
public class BlendKey {
    [SerializeField]
    public string soundName;
    [SerializeField]
    public List<String> neemList;

    public override bool Equals(object obj) {
        if (obj == null)
            return false;
        BlendKey c = obj as BlendKey ;
        if ((System.Object)c == null)
            return false;
        return soundName.Equals(c.soundName);
   }

    public override int GetHashCode() {
        return soundName.GetHashCode();
    }
}


public class TestDic: MonoBehaviour {
    
    public Dictionary<BlendKey, string> testDic = new Dictionary<BlendKey, string>();
    
    private void Start() {
        BlendKey testKey = new BlendKey();
        testKey.soundName = "ABC";
        testKey.neemList = new List<string>{"1", "2"};
        testDic.Add(testKey, "Sound 1");
        
        BlendKey testKey2 = new BlendKey();
        testKey2.soundName = "XYZ";
        testKey2.neemList = new List<string>{"8", "9"};
        testDic.Add(testKey2, "Sound 2");

        BlendKey testKey3 = new BlendKey();
        testKey3.soundName = "ABC";
        testKey3.neemList = new List<string>{"1", "2"};
        
        Debug.Log(testDic[testKey]);
        Debug.Log(testDic[testKey2]);
        Debug.Log(testDic[testKey3]);
    }
}

This can be easily solved by your first bullet point. Just implement the override. You will need to come up with a way to identify NeemData List on your own since I do not know the structure of it. It might be as easy as just taking the length of the array (might not be 100% unique but that would mean you first need to have the same soundname with the same amount of sound clips).

[Serializable]
public class BlendKey {
    [SerializeField]
    public string soundName;
    [SerializeField]
    public List<String> neemList;

    public override bool Equals(object obj) {
        if (obj == null)
            return false;
        BlendKey c = obj as BlendKey ;
        if ((System.Object)c == null)
            return false;
        return soundName.Equals(c.soundName);
   }

    public override int GetHashCode() {
        return soundName.GetHashCode();
    }
}


public class TestDic: MonoBehaviour {
    
    public Dictionary<BlendKey, string> testDic = new Dictionary<BlendKey, string>();
    
    private void Start() {
        BlendKey testKey = new BlendKey();
        testKey.soundName = "ABC";
        testKey.neemList = new List<string>{"1", "2"};
        testDic.Add(testKey, "Sound 1");
        
        BlendKey testKey2 = new BlendKey();
        testKey2.soundName = "XYZ";
        testKey2.neemList = new List<string>{"8", "9"};
        testDic.Add(testKey2, "Sound 2");

        BlendKey testKey3 = new BlendKey();
        testKey3.soundName = "ABC";
        testKey3.neemList = new List<string>{"1", "2"};
        
        Debug.Log(testDic[testKey]);
        Debug.Log(testDic[testKey2]);
        Debug.Log(testDic[testKey3]);
    }
}

This can be easily solved by your first bullet point. Just implement the override. You will need to come up with a way to identify NeemData List on your own since I do not know the structure of it. It might be as easy as just taking the length of the array (might not be 100% unique but that would mean you first need to have the same soundname with the same amount of sound clips).

[Serializable]
public class BlendKey {
    [SerializeField]
    public string soundName;
    [SerializeField]
    public List<String> neemList;

    public override bool Equals(object obj) {
        if (obj == null)
            return false;
        BlendKey c = obj as BlendKey ;
        if (c == null)
            return false;
        return soundName.Equals(c.soundName);
   }

    public override int GetHashCode() {
        return soundName.GetHashCode();
    }
}


public class TestDic: MonoBehaviour {
    
    public Dictionary<BlendKey, string> testDic = new Dictionary<BlendKey, string>();
    
    private void Start() {
        BlendKey testKey = new BlendKey();
        testKey.soundName = "ABC";
        testKey.neemList = new List<string>{"1", "2"};
        testDic.Add(testKey, "Sound 1");
        
        BlendKey testKey2 = new BlendKey();
        testKey2.soundName = "XYZ";
        testKey2.neemList = new List<string>{"8", "9"};
        testDic.Add(testKey2, "Sound 2");

        BlendKey testKey3 = new BlendKey();
        testKey3.soundName = "ABC";
        testKey3.neemList = new List<string>{"1", "2"};
        
        Debug.Log(testDic[testKey]);
        Debug.Log(testDic[testKey2]);
        Debug.Log(testDic[testKey3]);
    }
}
Source Link
Zibelas
  • 5k
  • 2
  • 15
  • 26

This can be easily solved by your first bullet point. Just implement the override. You will need to come up with a way to identify NeemData List on your own since I do not know the structure of it. It might be as easy as just taking the length of the array (might not be 100% unique but that would mean you first need to have the same soundname with the same amount of sound clips).

[Serializable]
public class BlendKey {
    [SerializeField]
    public string soundName;
    [SerializeField]
    public List<String> neemList;

    public override bool Equals(object obj) {
        if (obj == null)
            return false;
        BlendKey c = obj as BlendKey ;
        if ((System.Object)c == null)
            return false;
        return soundName.Equals(c.soundName);
   }

    public override int GetHashCode() {
        return soundName.GetHashCode();
    }
}


public class TestDic: MonoBehaviour {
    
    public Dictionary<BlendKey, string> testDic = new Dictionary<BlendKey, string>();
    
    private void Start() {
        BlendKey testKey = new BlendKey();
        testKey.soundName = "ABC";
        testKey.neemList = new List<string>{"1", "2"};
        testDic.Add(testKey, "Sound 1");
        
        BlendKey testKey2 = new BlendKey();
        testKey2.soundName = "XYZ";
        testKey2.neemList = new List<string>{"8", "9"};
        testDic.Add(testKey2, "Sound 2");

        BlendKey testKey3 = new BlendKey();
        testKey3.soundName = "ABC";
        testKey3.neemList = new List<string>{"1", "2"};
        
        Debug.Log(testDic[testKey]);
        Debug.Log(testDic[testKey2]);
        Debug.Log(testDic[testKey3]);
    }
}