コード例 #1
0
    // for some reason Unity doesn't like these functions
    // packs data and returns it as byte array
    // public static byte[] PackToBytes(GameObject entity, string prefabPath = "")
    // {
    //     SerializedObject obj = Pack(entity, prefabPath);
    //     byte[] data = FileStream.SerializeObject(entity);
    //     return data;
    // }
    //
    // // packs data and returns it as byte array
    // public byte[] PackToBytes(string prefabPath = "")
    // {
    //     return PackToBytes(gameObject, prefabPath);
    // }

    // the unpack functions are all static since these are meant to load in game objects
    // unpacks the serialized object
    // there's no local Unpack since a new object would need to be created, then its values would need to be instaniated.
    public static GameObject Unpack(SerializedObject serializedObject)
    {
        // if the serialized object is empty, return null.
        if (serializedObject == null)
        {
            return(null);
        }

        // game object
        GameObject gameObject;

        // PARENT //
        // there is a prefab - load prefab
        if (serializedObject.prefabPath != "")
        {
            Object newObject = Resources.Load(serializedObject.prefabPath);
            gameObject      = Instantiate((GameObject)newObject);
            gameObject.name = serializedObject.name;
        }
        else // there is no prefab - load from type
        {
            gameObject = new GameObject(serializedObject.name);
        }

        // sets values for transformation
        gameObject.transform.position   = SerializedObject.Vec3ToUnityVector3(serializedObject.position);
        gameObject.transform.rotation   = SerializedObject.QuatToQuaternion(serializedObject.rotation);
        gameObject.transform.localScale = SerializedObject.Vec3ToUnityVector3(serializedObject.localScale);

        // COMPONENTS //
        // gets and adds components, giving them the overwritten data
        foreach (SerializedComponent comp in serializedObject.components)
        {
            // generates a serialized object from the component.
            // object objectComp = System.Activator.CreateInstance(comp.type);
            Component attachedComp = null; // component

            // if there is no type, move on to the next one.
            if (comp.type == null)
            {
                continue;
            }

            // checks to see if the component is already attached
            // (Name returns class name, .FullName returns class namespace with class name)
            attachedComp = gameObject.GetComponent(comp.type.FullName);

            // if the attached component is null, it means it hasn't actually been attached at.
            if (attachedComp == null)
            {
                attachedComp = gameObject.AddComponent(comp.type); // attaches component
            }
            // if the component is a serializable object, call its function.
            if (attachedComp is SerializableObject)
            {
                SerializableObject serialObject = (SerializableObject)(attachedComp);
                serialObject.ImportSerializedComponent(serializedObject, comp); // passes content so it can be downcasted.
            }
        }

        // CHILDREN //
        // adds the children
        foreach (SerializedObject child in serializedObject.children)
        {
            GameObject importedChild = SerializableObject.Unpack(child);
            importedChild.transform.parent = gameObject.transform;
        }

        return(gameObject);
    }