复制ScriptableObjects

时间:2013-03-10 03:02:52

标签: unity3d

有没有办法在ScriptableObjects中模仿MonoBehaviour复制语义?

说我有这样的MonoBehaviour:

public class DummyClassBehaviour : MonoBehaviour {
    public DummyClass           DummyClassTest;     //ScriptableObject
    public DummyClassBehaviour  DummyBehaviourTest; //Another DummyClassBehaviour
}

和ScriptableObject:

public class DummyClass : ScriptableObject {
    public string Text = "";
}

当我复制(CTRL + D)附加了DummyClassBehaviour的GameObject时,'DummyBehaviourTest'会按照您的预期复制:如果它在我正在复制的GameObject中引用MonoBehaviour,则复制机制会更新对同一MonoBehavi的引用输入新的GameObject。如果它在另一个GameObject中引用MonoBehaviour,则该引用保持不变。

另一方面,ScriptableObject始终引用原始对象。所以我最终得到了 N GameObject,它们都是从原始GameObject共享相同的ScriptableObject(DummyClass)。我正在使用ScriptableObjects来允许非Monobehaviour数据类的序列化。

1 个答案:

答案 0 :(得分:2)

据我所知,如果我错了,请有人纠正我,你不能修改ScriptableObject的序列化行为以匹配MonoBehaviour的序列化行为。即,如果复制,它应该更新引用。

相反,我选择了不太理想的解决方案,但它确实有效。我的类被分配了一个唯一的标识符,与其他所有标识符一样被序列化我在DummyBehaviour.Awake()中使用此ID来创建一个查找表,然后我可以用它来重新分配我的DummyClass。

我不会接受我自己的答案,因为我觉得它没有完全回答我原来的问题,但它是相关的:

[System.Serializable]
public class DummyClass {
// Unique id is assigned by DummyBehaviour and is unique to the game object
    // that DummyBehaviour is attached to.
public int UniqueID = -1;
public string Text = "";

// Override GetHashCode so Dictionary lookups 
public override int GetHashCode(){
    int hash = 17;
        hash = hash * 31 + UniqueID;
    return hash;
}

    // override equality function, allows dictionary to do comparisons.
public override bool Equals(object obj)
{
    if (object.ReferenceEquals(obj, null))return false;

    DummyClass item = obj as DummyClass;
    return item.UniqueID == this.UniqueID;
}

    // Allow checks of the form 'if(dummyClass)'
public static implicit operator bool(DummyClass a) 
{
    if (object.ReferenceEquals(a, null)) return false;
    return (a.UniqueID==-1)?false:true;
}


public static bool operator ==(DummyClass a, DummyClass b)
{
    if (object.ReferenceEquals(a, null))
    {
         return object.ReferenceEquals(b, null);
    }

    return a.Equals(b);
}
public static bool operator !=(DummyClass a, DummyClass b)
{
    if (object.ReferenceEquals(a, null))
    {
         return object.ReferenceEquals(b, null);
    }
    return !a.Equals(b);
}
}

我的MonoBehaviour:

[ExecuteInEditMode]
public class DummyBehaviour : MonoBehaviour {
    public List<DummyClass> DummyClasses = new List<DummyClass>();

    // reassign references based on uniqueid.
    void Awake(){   
        Dictionary<DummyClass,DummyClass> dmap = new Dictionary<DummyClass,DummyClass>();
        // iterate over all dummyclasses, reassign references.
        for(int i = 0; i < DummyClasses.Count; i++){
            DummyClass2 d = DummyClasses[i];
            if(dmap.ContainsKey(d)){
                DummyClasses[i] = dmap[d];
            } else {
                dmap[d] = d;
            }
        }
        DummyClasses[0].Text = "All items same";
    }

    // helper function, for inspector contextmenu, to add more classes from Editor
    [ContextMenu ("AddDummy")]
    void AddDummy(){
        if(DummyClasses.Count==0)DummyClasses.Add(new DummyClass{UniqueID = 1});
        else {
            // Every item after 0 points to zero, serialization will remove refs during deep copy.
            DummyClasses.Add(DummyClasses[0]);
        }
        UnityEditor.EditorUtility.SetDirty(this);
    }
}