unity3d – 复制ScriptableObjects

有没有一种方法可以在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,则复制机制会更新对新的相同MonoBehaviour类型的引用游戏对象.如果它在另一个GameObject中引用MonoBehaviour,则该引用保持不变.

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

最佳答案 据我所知,如果我错了,请有人纠正我,你不能修改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);
    }
}
点赞