using UnityEngine;

namespace XGame.Framework.Asset
{
    /// <summary>
    /// UnityEngine.Object资源
    /// </summary>
    internal class ObjectAsset : Asset
    {
        public override AssetType AssetType => AssetType.UniObject;
        #region 重载
        public override object Clone()
        {
            var result = Source;
            if (IsCloneable)
            {
                Object obj = Source as Object;
                if (obj != null)
                {
                    try
                    {
                        result = Object.Instantiate(obj);
                    }
                    catch (System.Exception ex)
                    {
                        result = null;
                        Log.Exception($"Object Instantiate Exception. Name:{AddressableName} Type:{obj.GetType()}", ex);
                    }
                    
                }
            }
            if (result != null)
                Retain();
            return result;
        }
        protected override void OnSetSource()
        {
            IsCloneable = Source != null &&
            (Source is Component || Source is GameObject/* || Source is AssetBundle*/);
        }
        protected override void OnInit()
        {

        }

        protected override void OnRelease()
        {

        }

        protected override void OnRetain()
        {

        }

        protected override void OnDispose()
        {
            if (Source != null)
            { // 非克隆资源，Mesh,Texture,Material,Shader等
              //需要使用Resources.UnloadAsset卸载
                if (!IsCloneable)
                    Resources.UnloadAsset(Source as Object);
            }
        }

        #endregion
    }
}

