Esempio n. 1
0
    static public void CopyComponents(Slicer2D slicer, GameObject gObject)
    {
        Component[] scriptList = slicer.gameObject.GetComponents <Component>();
        foreach (Component script in scriptList)
        {
            if (script == null)
            {
                continue;
            }
            // Do not copy Colliders
            if (script.GetType().ToString() == "UnityEngine.PolygonCollider2D" || script.GetType().ToString() == "UnityEngine.EdgeCollider2D" || script.GetType().ToString() == "UnityEngine.BoxCollider2D" || script.GetType().ToString() == "UnityEngine.CircleCollider2D" || script.GetType().ToString() == "UnityEngine.CapsuleCollider2D")
            {
                continue;
            }

            switch (slicer.textureType)
            {
            case TextureType.SpriteAnimation:
                if (script.GetType().ToString() == "UnityEngine.SpriteRenderer" || script.GetType().ToString() == "UnityEngine.Animator")
                {
                    continue;
                }
                break;

            case TextureType.Sprite:
                if (script.GetType().ToString() == "UnityEngine.SpriteRenderer")
                {
                    continue;
                }
                break;

            default:
                break;
            }

            if (script.GetType().ToString() != "UnityEngine.Transform")
            {
                gObject.AddComponent(script.GetType());
                System.Reflection.FieldInfo[] fields = script.GetType().GetFields();

                foreach (System.Reflection.FieldInfo field in fields)
                {
                    field.SetValue(gObject.GetComponent(script.GetType()), field.GetValue(script));
                }
            }
        }

        foreach (Behaviour childCompnent in gObject.GetComponentsInChildren <Behaviour>())
        {
            foreach (Behaviour child in slicer.GetComponentsInChildren <Behaviour>())
            {
                if (child.GetType() == childCompnent.GetType())
                {
                    childCompnent.enabled = child.enabled;
                    break;
                }
            }
        }
    }
Esempio n. 2
0
    static public void Copy(Slicer2D slicer, GameObject gObject)
    {
        Component[] scriptList = slicer.gameObject.GetComponents <Component>();
        Component   script;

        System.Reflection.FieldInfo[] fields;
        System.Reflection.FieldInfo   field;

        System.Type objectType;
        string      objectString;

        for (int i = 0; i < scriptList.Length; i++)
        {
            script = scriptList[i];

            if (script == null)
            {
                continue;
            }

            objectType   = script.GetType();
            objectString = objectType.ToString();

            // Do not copy Colliders
            switch (objectString)
            {
            case "UnityEngine.Transform":

            case "UnityEngine.PolygonCollider2D":
            case "UnityEngine.EdgeCollider2D":
            case "UnityEngine.BoxCollider2D":
            case "UnityEngine.CircleCollider2D":
            case "UnityEngine.CapsuleCollider2D":
                continue;
            }

            switch (slicer.textureType)
            {
            case Slicer2D.TextureType.SpriteAnimation:
                if (objectString == "UnityEngine.SpriteRenderer" || objectString == "UnityEngine.Animator")
                {
                    continue;
                }
                break;

            case Slicer2D.TextureType.Sprite:
                if (objectString == "UnityEngine.SpriteRenderer")
                {
                    continue;
                }
                break;

            default:
                break;
            }


            switch (objectString)
            {
            case "UnityEngine.SpringJoint2D":
                SpringJoint2D orgingSpringJoint2D = (SpringJoint2D)script;
                SpringJoint2D springJoint2D       = gObject.AddComponent <SpringJoint2D>();

                springJoint2D.connectedBody = orgingSpringJoint2D.connectedBody;
                continue;
            }

            gObject.AddComponent(objectType);

            fields = objectType.GetFields();

            for (int x = 0; x < fields.Length; x++)
            {
                field = fields[x];

                field.SetValue(gObject.GetComponent(objectType), field.GetValue(script));
            }
        }

        Behaviour[] children       = gObject.GetComponentsInChildren <Behaviour>();
        Behaviour[] slicerChildren = slicer.GetComponentsInChildren <Behaviour>();
        System.Type componentType;

        foreach (Behaviour childCompnent in children)
        {
            componentType = childCompnent.GetType();

            foreach (Behaviour child in slicerChildren)
            {
                if (child.GetType() == componentType)
                {
                    childCompnent.enabled = child.enabled;
                    break;
                }
            }
        }
    }