/// <summary>Instantiate tree objects.</summary>
        /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>
        /// <param name="setActive">Set activates/deactivates child GameObject.</param>
        public GameObject Instantiate(TransformCloneType cloneType, bool setActive)
        {
            var root = UnityEngine.Object.Instantiate(original) as GameObject;

            InstantiateChildren(root, cloneType, setActive);
            return(root);
        }
 void InstantiateChildren(GameObject root, TransformCloneType cloneType, bool?setActive)
 {
     foreach (var child in children)
     {
         var childRoot = root.Add(child.original, cloneType, setActive);
         child.InstantiateChildren(childRoot, cloneType, setActive);
     }
 }
Esempio n. 3
0
 void InstantiateChildren(GameObject root, TransformCloneType cloneType, bool? setActive)
 {
     foreach (var child in children)
     {
         var childRoot = root.Add(child.original, cloneType, setActive);
         child.InstantiateChildren(childRoot, cloneType, setActive);
     }
 }
        /// <summary>
        /// <para>Adds the GameObject as children of this GameObject. Target is cloned.</para>
        /// </summary>
        /// <param name="parent">Parent GameObject.</param>
        /// <param name="childOriginal">Clone Target.</param>
        /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>
        /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
        /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
        public static GameObject Add(this GameObject parent, GameObject childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null)
        {
            if (parent == null) throw new ArgumentNullException("parent");
            if (childOriginal == null) throw new ArgumentNullException("childOriginal");

            var child = UnityEngine.Object.Instantiate(childOriginal) as GameObject;
            // for uGUI, should use SetParent(parent, false)
            var childTransform = child.transform;
            #if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
            var rectTransform = childTransform as RectTransform;
            if (rectTransform != null)
            {
                rectTransform.SetParent(parent.transform, worldPositionStays: false);
            }
            else
            {
            #endif
                var parentTransform = parent.transform;
                childTransform.parent = parentTransform;
                switch (cloneType)
                {
                    case TransformCloneType.FollowParent:
                        childTransform.localPosition = parentTransform.localPosition;
                        childTransform.localScale = parentTransform.localScale;
                        childTransform.localRotation = parentTransform.localRotation;
                        break;
                    case TransformCloneType.Origin:
                        childTransform.localPosition = Vector3.zero;
                        childTransform.localScale = Vector3.one;
                        childTransform.localRotation = Quaternion.identity;
                        break;
                    case TransformCloneType.KeepOriginal:
                        var childOriginalTransform = childOriginal.transform;
                        childTransform.localPosition = childOriginalTransform.localPosition;
                        childTransform.localScale = childOriginalTransform.localScale;
                        childTransform.localRotation = childOriginalTransform.localRotation;
                        break;
                    case TransformCloneType.DoNothing:
                    default:
                        break;
                }
            #if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
            }
            #endif
            child.layer = parent.layer;

            if (setActive != null)
            {
                child.SetActive(setActive.Value);
            }
            if (specifiedName != null)
            {
                child.name = specifiedName;
            }

            return child;
        }
        /// <summary>
        /// <para>Adds the GameObject/Component as the first children of this GameObject. Target is cloned.</para>
        /// </summary>
        /// <param name="parent">Parent GameObject.</param>
        /// <param name="childOriginal">Clone Target.</param>
        /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>
        /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
        /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
        /// <param name="setLayer">Set layer of child GameObject same with parent.</param>
        public static T AddFirst <T>(this GameObject parent, T childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool?setActive = null, string specifiedName = null, bool setLayer = false)
            where T : UnityEngine.Object
        {
            var child = Add(parent, childOriginal, cloneType, setActive, specifiedName, setLayer);
            var go    = GetGameObject(child);

            if (go == null)
            {
                return(child);
            }

            go.transform.SetAsFirstSibling();
            return(child);
        }
        /// <summary>
        /// <para>Adds the GameObject/Component after this GameObject. Target is cloned.</para>
        /// </summary>
        /// <param name="parent">Parent GameObject.</param>
        /// <param name="childOriginal">Clone Target.</param>
        /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>
        /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
        /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
        /// <param name="setLayer">Set layer of child GameObject same with parent.</param>
        public static T AddAfterSelf <T>(this GameObject parent, T childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool?setActive = null, string specifiedName = null, bool setLayer = false)
            where T : UnityEngine.Object
        {
            var root = parent.Parent();

            if (root == null)
            {
                throw new InvalidOperationException("The parent root is null");
            }

            var sibilingIndex = parent.transform.GetSiblingIndex() + 1;
            var child         = Add(root, childOriginal, cloneType, setActive, specifiedName, setLayer);
            var go            = GetGameObject(child);

            if (go == null)
            {
                return(child);
            }

            go.transform.SetSiblingIndex(sibilingIndex);
            return(child);
        }
        public static List <GameObject> Add(this GameObject parent, IEnumerable <GameObject> childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool?setActive = null, string specifiedName = null)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }
            if (childOriginals == null)
            {
                throw new ArgumentNullException("childOriginals");
            }

            var list = new List <GameObject>();

            foreach (var childOriginal in childOriginals)
            {
                var child = Add(parent, childOriginal, cloneType, setActive, specifiedName);
                list.Add(child);
            }

            return(list);
        }
        /// <summary>
        /// <para>Adds the GameObject as children of this GameObject. Target is cloned.</para>
        /// </summary>
        /// <param name="parent">Parent GameObject.</param>
        /// <param name="childOriginal">Clone Target.</param>
        /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>
        /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
        /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
        public static GameObject Add(this GameObject parent, GameObject childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool?setActive = null, string specifiedName = null)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }
            if (childOriginal == null)
            {
                throw new ArgumentNullException("childOriginal");
            }

            var child = UnityEngine.Object.Instantiate(childOriginal) as GameObject;

            // Unity 4.6, we can use SetParent but before that can't therefore use set localPosition/Rotation/Scale.
            child.transform.parent = parent.transform;
            child.layer            = parent.layer;

            switch (cloneType)
            {
            case TransformCloneType.FollowParent:
                child.transform.localPosition = parent.transform.localPosition;
                child.transform.localScale    = parent.transform.localScale;
                child.transform.localRotation = parent.transform.localRotation;
                break;

            case TransformCloneType.Origin:
                child.transform.localPosition = Vector3.zero;
                child.transform.localScale    = Vector3.one;
                child.transform.localRotation = Quaternion.identity;
                break;

            case TransformCloneType.KeepOriginal:
                child.transform.localPosition = childOriginal.transform.localPosition;
                child.transform.localScale    = childOriginal.transform.localScale;
                child.transform.localRotation = childOriginal.transform.localRotation;
                break;

            case TransformCloneType.DoNothing:
            default:
                break;
            }

            if (setActive != null)
            {
                child.SetActive(setActive.Value);
            }
            if (specifiedName != null)
            {
                child.name = specifiedName;
            }

            return(child);
        }
        /// <summary>
        /// <para>Adds the GameObject/Component as the first children of this GameObject. Target is cloned.</para>
        /// </summary>
        /// <param name="parent">Parent GameObject.</param>
        /// <param name="childOriginals">Clone Target.</param>
        /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>
        /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
        /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
        /// <param name="setLayer">Set layer of child GameObject same with parent.</param>
        public static T[] AddFirstRange <T>(this GameObject parent, IEnumerable <T> childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool?setActive = null, string specifiedName = null, bool setLayer = false)
            where T : UnityEngine.Object
        {
            var child = AddRange(parent, childOriginals, cloneType, setActive, specifiedName, setLayer);

            for (int i = child.Length - 1; i >= 0; i--)
            {
                var go = GetGameObject(child[i]);
                if (go == null)
                {
                    continue;
                }
                go.transform.SetAsFirstSibling();
            }
            return(child);
        }
        public static List <GameObject> AddAfterSelf(this GameObject parent, IEnumerable <GameObject> childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool?setActive = null, string specifiedName = null)
        {
            var root = parent.Parent();

            if (root == null)
            {
                throw new InvalidOperationException("The parent root is null");
            }

            var sibilingIndex = parent.transform.GetSiblingIndex() + 1;
            var child         = Add(root, childOriginals, cloneType, setActive, specifiedName);

            for (int i = child.Count - 1; i >= 0; i--)
            {
                child[i].transform.SetSiblingIndex(sibilingIndex);
            }

            return(child);
        }
        /// <summary>
        /// <para>Adds the GameObject/Component as children of this GameObject. Target is cloned.</para>
        /// </summary>
        /// <param name="parent">Parent GameObject.</param>
        /// <param name="childOriginals">Clone Target.</param>
        /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>
        /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
        /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
        public static T[] AddRange <T>(this GameObject parent, IEnumerable <T> childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool?setActive = null, string specifiedName = null, bool setLayer = false)
            where T : UnityEngine.Object
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }
            if (childOriginals == null)
            {
                throw new ArgumentNullException("childOriginals");
            }

            // iteration optimize
            {
                var array = childOriginals as T[];
                if (array != null)
                {
                    var result = new T[array.Length];
                    for (int i = 0; i < array.Length; i++)
                    {
                        var child = Add(parent, array[i], cloneType, setActive, specifiedName, setLayer);
                        result[i] = child;
                    }
                    return(result);
                }
            }

            {
                var iterList = childOriginals as IList <T>;
                if (iterList != null)
                {
                    var result = new T[iterList.Count];
                    for (int i = 0; i < iterList.Count; i++)
                    {
                        var child = Add(parent, iterList[i], cloneType, setActive, specifiedName, setLayer);
                        result[i] = child;
                    }
                    return(result);
                }
            }

            {
                var result = new List <T>();
                foreach (var childOriginal in childOriginals)
                {
                    var child = Add(parent, childOriginal, cloneType, setActive, specifiedName, setLayer);
                    result.Add(child);
                }

                return(result.ToArray());
            }
        }
        public static List <GameObject> AddFirst(this GameObject parent, IEnumerable <GameObject> childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool?setActive = null, string specifiedName = null)
        {
            var child = Add(parent, childOriginals, cloneType, setActive, specifiedName);

            for (int i = child.Count - 1; i >= 0; i--)
            {
                child[i].transform.SetAsFirstSibling();
            }
            return(child);
        }
        public static GameObject AddAfterSelf(this GameObject parent, GameObject childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool?setActive = null, string specifiedName = null)
        {
            var root = parent.Parent();

            if (root == null)
            {
                throw new InvalidOperationException("The parent root is null");
            }

            var sibilingIndex = parent.transform.GetSiblingIndex() + 1;
            var child         = Add(root, childOriginal, cloneType, setActive, specifiedName);

            child.transform.SetSiblingIndex(sibilingIndex);
            return(child);
        }
        /// <summary>
        /// <para>Adds the GameObject as children of this GameObject. Target is cloned.</para>
        /// </summary>
        /// <param name="parent">Parent GameObject.</param>
        /// <param name="childOriginals">Clone Target.</param>
        /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>
        /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
        /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
        public static List<GameObject> Add(this GameObject parent, IEnumerable<GameObject> childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null)
        {
            if (parent == null) throw new ArgumentNullException("parent");
            if (childOriginals == null) throw new ArgumentNullException("childOriginals");

            var list = new List<GameObject>();
            foreach (var childOriginal in childOriginals)
            {
                var child = Add(parent, childOriginal, cloneType, setActive, specifiedName);
                list.Add(child);
            }

            return list;
        }
        public static GameObject AddFirst(this GameObject parent, GameObject childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool?setActive = null, string specifiedName = null)
        {
            var child = Add(parent, childOriginal, cloneType, setActive, specifiedName);

            child.transform.SetAsFirstSibling();
            return(child);
        }
        /// <summary>
        /// <para>Adds the GameObject/Component after this GameObject. Target is cloned.</para>
        /// </summary>
        /// <param name="parent">Parent GameObject.</param>
        /// <param name="childOriginals">Clone Target.</param>
        /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>
        /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
        /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
        /// <param name="setLayer">Set layer of child GameObject same with parent.</param>
        public static T[] AddAfterSelfRange <T>(this GameObject parent, IEnumerable <T> childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool?setActive = null, string specifiedName = null, bool setLayer = false)
            where T : UnityEngine.Object
        {
            var root = parent.Parent();

            if (root == null)
            {
                throw new InvalidOperationException("The parent root is null");
            }

            var sibilingIndex = parent.transform.GetSiblingIndex() + 1;
            var child         = AddRange(root, childOriginals, cloneType, setActive, specifiedName, setLayer);

            for (int i = child.Length - 1; i >= 0; i--)
            {
                var go = GetGameObject(child[i]);
                if (go == null)
                {
                    continue;
                }
                go.transform.SetSiblingIndex(sibilingIndex);
            }

            return(child);
        }
 /// <summary>
 /// <para>Adds the GameObject as the first children of this GameObject. Target is cloned.</para>
 /// </summary>
 /// <param name="parent">Parent GameObject.</param>
 /// <param name="childOriginals">Clone Target.</param>
 /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>       
 /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
 /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
 public static List<GameObject> AddFirst(this GameObject parent, IEnumerable<GameObject> childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null)
 {
     var child = Add(parent, childOriginals, cloneType, setActive, specifiedName);
     for (int i = child.Count - 1; i >= 0; i--)
     {
         child[i].transform.SetAsFirstSibling();
     }
     return child;
 }
 /// <summary>
 /// <para>Adds the GameObject as the first children of this GameObject. Target is cloned.</para>
 /// </summary>
 /// <param name="parent">Parent GameObject.</param>
 /// <param name="childOriginal">Clone Target.</param>
 /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>      
 /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
 /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
 public static GameObject AddFirst(this GameObject parent, GameObject childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null)
 {
     var child = Add(parent, childOriginal, cloneType, setActive, specifiedName);
     child.transform.SetAsFirstSibling();
     return child;
 }
        /// <summary>
        /// <para>Adds the GameObject before this GameObject. Target is cloned.</para>
        /// </summary>
        /// <param name="parent">Parent GameObject.</param>
        /// <param name="childOriginals">Clone Target.</param>
        /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>     
        /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
        /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
        public static List<GameObject> AddBeforeSelf(this GameObject parent, IEnumerable<GameObject> childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null)
        {
            var root = parent.Parent();
            if (root == null) throw new InvalidOperationException("The parent root is null");

            var sibilingIndex = parent.transform.GetSiblingIndex();
            var child = Add(root, childOriginals, cloneType, setActive, specifiedName);
            for (int i = child.Count - 1; i >= 0; i--)
            {
                child[i].transform.SetSiblingIndex(sibilingIndex);
            }

            return child;
        }
        /// <summary>
        /// <para>Adds the GameObject before this GameObject. Target is cloned.</para>
        /// </summary>
        /// <param name="parent">Parent GameObject.</param>
        /// <param name="childOriginal">Clone Target.</param>
        /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>     
        /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
        /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
        public static GameObject AddBeforeSelf(this GameObject parent, GameObject childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null)
        {
            var root = parent.Parent();
            if (root == null) throw new InvalidOperationException("The parent root is null");

            var sibilingIndex = parent.transform.GetSiblingIndex();

            var child = Add(root, childOriginal, cloneType, setActive, specifiedName);
            child.transform.SetSiblingIndex(sibilingIndex);
            return child;
        }
        /// <summary>
        /// <para>Adds the GameObject/Component as children of this GameObject. Target is cloned.</para>
        /// </summary>
        /// <param name="parent">Parent GameObject.</param>
        /// <param name="childOriginal">Clone Target.</param>
        /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>
        /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param>
        /// <param name="specifiedName">Set name of child GameObject. If null, doesn't set specified value.</param>
        /// <param name="setLayer">Set layer of child GameObject same with parent.</param>
        public static T Add <T>(this GameObject parent, T childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool?setActive = null, string specifiedName = null, bool setLayer = false)
            where T : UnityEngine.Object
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }
            if (childOriginal == null)
            {
                throw new ArgumentNullException("childOriginal");
            }

            var child = UnityEngine.Object.Instantiate(childOriginal);

            var childGameObject = GetGameObject(child);

            // for uGUI, should use SetParent(parent, false)
            var childTransform = childGameObject.transform;

#if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
            var rectTransform = childTransform as RectTransform;
            if (rectTransform != null)
            {
                rectTransform.SetParent(parent.transform, worldPositionStays: false);
            }
            else
            {
#endif
            var parentTransform = parent.transform;
            childTransform.parent = parentTransform;
            switch (cloneType)
            {
            case TransformCloneType.FollowParent:
                childTransform.localPosition = parentTransform.localPosition;
                childTransform.localScale    = parentTransform.localScale;
                childTransform.localRotation = parentTransform.localRotation;
                break;

            case TransformCloneType.Origin:
                childTransform.localPosition = Vector3.zero;
                childTransform.localScale    = Vector3.one;
                childTransform.localRotation = Quaternion.identity;
                break;

            case TransformCloneType.KeepOriginal:
                var co = GetGameObject(childOriginal);
                var childOriginalTransform = co.transform;
                childTransform.localPosition = childOriginalTransform.localPosition;
                childTransform.localScale    = childOriginalTransform.localScale;
                childTransform.localRotation = childOriginalTransform.localRotation;
                break;

            case TransformCloneType.DoNothing:
            default:
                break;
            }
#if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
        }
#endif
            if (setLayer)
            {
                childGameObject.layer = parent.layer;
            }

            if (setActive != null)
            {
                childGameObject.SetActive(setActive.Value);
            }
            if (specifiedName != null)
            {
                child.name = specifiedName;
            }

            return(child);
        }
Esempio n. 22
0
 /// <summary>Instantiate tree objects.</summary>
 /// <param name="cloneType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param>     
 /// <param name="setActive">Set activates/deactivates child GameObject.</param>
 public GameObject Instantiate(TransformCloneType cloneType, bool setActive)
 {
     var root = UnityEngine.Object.Instantiate(original) as GameObject;
     InstantiateChildren(root, cloneType, setActive);
     return root;
 }