示例#1
0
        /// <summary>
        /// Clones the object original, places it at position and sets the rotation to rotation, then returns the cloned object.
        /// This is essentially the same as using duplicate command (cmd-d) in Unity and then moving the object to the given location.
        /// If a game object, component or script instance is passed, Instantiate will clone the entire game object hierarchy, with all children cloned as well.
        /// All game objects are activated.
        /// </summary>
        /// <param name="original"></param>
        public static UnityObject Instantiate(UnityObject original)
        {
            if (original == null)
            {
                return(null);
            }
            GameObject clone = null;

            if (original is Component)
            {
                clone = (original as Component).gameObject.Clone() as GameObject;
            }
            else if (original is GameObject)
            {
                GameObject toClone = (original as GameObject).transform.root.gameObject;
                //GameObject toClone = original as GameObject;
                clone = toClone.Clone() as GameObject;
            }
            else
            {
                return(original.Clone());
            }
            // NOTE: It is very important that this is done at the end otherwise we cannot find the correct object to return.
            Dictionary <int, UnityObject> idMap = new Dictionary <int, UnityObject>();

            clone.SetNewId(idMap);
            clone.FixReferences(idMap);

            Application.RegisterComponents(newInstantiatedComponents);
            Application.AwakeNewComponents();

            return(idMap[original.GetInstanceID()]);
        }