有没有办法模仿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。如果它在另一个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);
    }
}

09-06 04:33