using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace FairyGUI
{
    /// 
    /// GoWrapper is class for wrapping common gameobject into UI display list.
    /// 
    public class GoWrapper : DisplayObject
    {
        [Obsolete("No need to manually set this flag anymore, coz it will be handled automatically.")]
        public bool supportStencil;
        public event Action onUpdate;
        protected GameObject _wrapTarget;
        protected List _renderers;
        protected Dictionary _materialsBackup;
        protected Canvas _canvas;
        protected bool _cloneMaterial;
        protected bool _shouldCloneMaterial;
        protected struct RendererInfo
        {
            public Renderer renderer;
            public Material[] materials;
            public int sortingOrder;
        }
        protected static List helperTransformList = new List();
        /// 
        /// 
        /// 
        public GoWrapper()
        {
            // _flags |= Flags.SkipBatching;
            _renderers = new List();
            _materialsBackup = new Dictionary();
            CreateGameObject("GoWrapper");
        }
        /// 
        /// 
        /// 
        /// 包装对象。
        public GoWrapper(GameObject go) : this()
        {
            SetWrapTarget(go, false);
        }
        /// 
        /// 设置包装对象。注意如果原来有包装对象,设置新的包装对象后,原来的包装对象只会被删除引用,但不会被销毁。
        /// 对象包含的所有材质不会被复制,如果材质已经是公用的,这可能影响到其他对象。如果希望自动复制,改为使用SetWrapTarget(target, true)设置。
        /// 
        public GameObject wrapTarget
        {
            get { return _wrapTarget; }
            set { SetWrapTarget(value, false); }
        }
        [Obsolete("setWrapTarget is deprecated. Use SetWrapTarget instead.")]
        public void setWrapTarget(GameObject target, bool cloneMaterial)
        {
            SetWrapTarget(target, cloneMaterial);
        }
        /// 
        ///  设置包装对象。注意如果原来有包装对象,设置新的包装对象后,原来的包装对象只会被删除引用,但不会被销毁。
        /// 
        /// 
        /// 如果true,则复制材质,否则直接使用sharedMaterial。
        public void SetWrapTarget(GameObject target, bool cloneMaterial)
        {
            // set Flags.SkipBatching only target not null
            if (target == null) _flags &= ~Flags.SkipBatching;
            else _flags |= Flags.SkipBatching;
            InvalidateBatchingState();
            RecoverMaterials();
            _cloneMaterial = cloneMaterial;
            if (_wrapTarget != null)
                _wrapTarget.transform.SetParent(null, false);
            _canvas = null;
            _wrapTarget = target;
            _shouldCloneMaterial = false;
            _renderers.Clear();
            if (_wrapTarget != null)
            {
                _wrapTarget.transform.SetParent(this.cachedTransform, false);
                _canvas = _wrapTarget.GetComponent