unity3d 小而精的克隆脚本。

脚本的主要作用是解决,unity3d下,预制不能相互引用的问题。

脚本在EditMode下,支持编辑时查看。

using System.Collections.Generic;
using UnityEngine;

namespace ExtraFoundation.Components
{
    //===============================================================================
    [ExecuteInEditMode]
    [AddComponentMenu("Pokemon/DuplicatorElement")]
    public sealed class DuplicatorElement : MonoBehaviour
    {
        #region Public Methods
        //---------------------------------------------------------------------------
        public GameObject[] GetCloneObjects()
        {
            if (cloneObjects.Count == 0)
            {
                return null;
            }

            return cloneObjects.ToArray();
        }
        #endregion

        #region Internal Methods
        //---------------------------------------------------------------------------
        private void Perform()
        {
            bool isPlaying = Application.isPlaying;

            if (templateObject == null || cloneCount <= 0)
            {
                return;
            }

            if (parentTrans == null)
            {
                parentTrans = gameObject.transform;
            }

            for (int i = 0; i < cloneObjects.Count; i++)
            {
                DestroyImmediate(cloneObjects[i]);
            }

            cloneObjects.Clear();

            for (int index = 0; index < cloneCount; index++)
            {
                GameObject go = GameObject.Instantiate(templateObject,
                    parentTrans) as GameObject;
                go.transform.localScale = Vector3.one;
                go.transform.localRotation = Quaternion.identity;
                go.transform.localPosition = Vector3.zero;

                go.name = string.Format("Element_{0}", index);
                if (!isPlaying)
                {
                    go.hideFlags = HideFlags.DontSave;
                }

                if (hideInitial)
                {
                    go.SetActive(false);
                }
                else
                {
                    go.SetActive(true);
                }

                cloneObjects.Add(go);
            }

            if (isPlaying && destroyTemplate)
            {
                DestroyImmediate(templateObject);
            }
        }
        #endregion

        #region Unity Methods
        //---------------------------------------------------------------------------
        private void Awake()
        {
            Perform();
        }

        //---------------------------------------------------------------------------
        private void Update()
        {
            if (Application.isPlaying)
            {
                return;
            }

            if (templateObject == null)
            {
                return;
            }

            if (cloneObjects.Count != cloneCount)
            {
                Perform();
            }
        }
        #endregion

        #region Internal Fields
        //---------------------------------------------------------------------------
        [SerializeField] private GameObject templateObject;

        //---------------------------------------------------------------------------
        [SerializeField] private Transform parentTrans;

        //---------------------------------------------------------------------------
        [SerializeField] private bool destroyTemplate;

        //---------------------------------------------------------------------------
        [SerializeField] private int cloneCount = 1;

        //---------------------------------------------------------------------------
        [SerializeField] private bool hideInitial;

        //---------------------------------------------------------------------------
        [SerializeField, HideInInspector]
        private List cloneObjects = new List();
        #endregion
    }
}

demo展示:

unity3d 小而精的克隆脚本。_第1张图片

被克隆的对象,可以是在预制内的gameObject也可以是一个预制。

通过这个脚本,就可以达到一个预制嵌套另外一个预制的效果。解决了预制不能互相引用的问题。

虽然脚本小,按照惯例,给一个参数方法说明。

------------------------重要参数方法-------------------------
templateObject                 //克隆模板的Object
parentTrans                       //克隆体的父节点
destroyTemplate               //是否在运行时删除克隆模板
cloneCount                        //克隆体总数量
hideInitial                          //克隆体的隐藏显示状态
cloneObjects                      //克隆体的List
GetCloneObjects               //方法:获取克隆体的List

你可能感兴趣的:(unity3d工具)