Exemplo n.º 1
0
        /// <summary>
        /// 同步从AssetBundle加载资源;
        /// </summary>
        /// <param name="path"></param>
        /// <param name="isUsePool"></param>
        /// <returns></returns>
        private AssetBundleAssetProxy LoadAssetSync(string path, bool isUsePool)
        {
            AssetBundleAssetProxy proxy = PoolMgr.Instance.GetCsharpObject <AssetBundleAssetProxy>();

            proxy.Initialize(path, isUsePool);

            Object      asset       = null;
            AssetBundle assetBundle = AssetBundleMgr.Instance.LoadFromFile(path);

            if (assetBundle != null)
            {
                var name = Path.GetFileNameWithoutExtension(path);
                asset = assetBundle.LoadAsset(name);
            }
            if (asset == null)
            {
                AssetBundleMgr.Instance.UnloadAsset(path, null);
                LogHelper.PrintError(string.Format("[ResourceMgr]LoadSyncAssetProxy load asset:{0} failure.", path));
            }
            else
            {
                AssetBundleMgr.Instance.AddAssetRef(path, asset);
            }
            proxy.OnFinish(asset);
            return(proxy);
        }
        private IEnumerator CoroutineWrap(IEnumerator coroutine)
        {
            var stacktrace = new StackTrace();

            var itor = coroutine;

            while (true)
            {
                bool state = false;
                try
                {
                    state = itor.MoveNext();
                }
                catch (Exception e)
                {
                    if (e is GameException)
                    {
                        (e as GameException).PrintException(stacktrace);
                    }
                    else
                    {
                        LogHelper.PrintError(e.ToString());
                    }
                    break;
                }
                if (!state)
                {
                    break;
                }
                yield return(WaitForEndOfFrame);
            }
        }
Exemplo n.º 3
0
        protected override void UpdateEx(float interval)
        {
            if (Reslut == BehaviorState.Running)
            {
                int count = _list.Count;
                if (_target == null)
                {
                    int index = Random.Range(0, count);
                    _target = _list[index];
                }
                switch (_target.Behave(Entity, interval))
                {
                case BehaviorState.Running:
                case BehaviorState.Finish:
                    Reslut = BehaviorState.Running;
                    break;

                case BehaviorState.Success:
                    Reslut = BehaviorState.Finish;
                    break;

                case BehaviorState.Failure:
                case BehaviorState.Reset:
                    Reslut = BehaviorState.Failure;
                    LogHelper.PrintError("[BTRandom]BTRandom execute failure!");
                    break;

                default:
                    Reslut = BehaviorState.Failure;
                    LogHelper.PrintError("[BTRandom]BTRandom execute failure!");
                    break;
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 添加事件;
        /// </summary>
        /// <param name="receiver">接收者</param>
        /// <param name="type">事件类型</param>
        /// <param name="callBack">事件回调</param>
        public void AddEvent(ObjectEx receiver, EventType type, EventHandler callBack)
        {
            if (null == receiver)
            {
                LogHelper.PrintError("[EventMgr]AddEvent error,the receiver is null.");
                return;
            }
            Dictionary <EventType, List <EventHandler> > dict;
            List <EventHandler> list;

            if (!EventDict.TryGetValue(receiver, out dict))
            {
                EventDict[receiver] = new Dictionary <EventType, List <EventHandler> >();
            }
            dict = EventDict[receiver];
            if (!dict.TryGetValue(type, out list))
            {
                list       = new List <EventHandler>();
                dict[type] = list;
            }
            list = dict[type];
            if (list.Contains(callBack))
            {
                LogHelper.PrintWarning(string.Format("[EventMgr]AddEvent repeat,receiver:{0},eventType:{1}.",
                                                     receiver.ID, type.ToString()));
            }
            else
            {
                list.Add(callBack);
            }
        }
        private static void InitDict(string path)
        {
            _rootBehavior = null;
            _behaviorDict.Clear();
            _connectionDict.Clear();
            TextAsset json           = Resources.Load <TextAsset>(path);
            string    content        = json.text.Replace("\r", "").Replace("\n", "");
            Hashtable table          = MiniJsonExtensions.hashtableFromJson(content);
            ArrayList nodeList       = table["nodes"] as ArrayList;
            ArrayList connectionList = table["connections"] as ArrayList;

            for (int i = 0; i < nodeList.Count; i++)
            {
                Hashtable nodeTable = nodeList[i] as Hashtable;
                var       id        = 0;
                if (int.TryParse(nodeTable["$id"].ToString(), out id))
                {
                    AbsBehavior absBehavior = CreateBehavior(nodeTable, id);
                    _behaviorDict[id] = absBehavior;
                    if (_rootBehavior == null)
                    {
                        _rootBehavior = absBehavior;
                    }
                    else
                    {
                        if (absBehavior.Id < _rootBehavior.Id)
                        {
                            _rootBehavior = absBehavior;
                        }
                    }
                }
                else
                {
                    LogHelper.PrintError("[BehaviorTreeFactory]try get node id error!");
                }
            }
            for (int i = 0; i < connectionList.Count; i++)
            {
                Hashtable connectionTable = connectionList[i] as Hashtable;
                int       source          = 0;
                int       target          = 0;
                Hashtable scurceNode      = connectionTable["_sourceNode"] as Hashtable;
                Hashtable targetNode      = connectionTable["_targetNode"] as Hashtable;
                if (int.TryParse(scurceNode["$ref"].ToString(), out source) &&
                    int.TryParse(targetNode["$ref"].ToString(), out target))
                {
                    List <int> list;
                    if (!_connectionDict.TryGetValue(source, out list))
                    {
                        _connectionDict[source] = new List <int>();
                        list = _connectionDict[source];
                    }
                    list.Add(target);
                }
                else
                {
                    LogHelper.PrintError("[BehaviorTreeFactory]try get source id and target id error!");
                }
            }
        }
Exemplo n.º 6
0
        /// Resource异步加载;
        private IEnumerator <float> LoadAsync(string path, ResourceAssetProxy proxy, Action <float> progress)
        {
            if (string.IsNullOrEmpty(path))
            {
                yield break;
            }
            ResourceRequest request = Resources.LoadAsync(path);

            while (request.progress < 0.99f)
            {
                progress?.Invoke(request.progress);
                yield return(Timing.WaitForOneFrame);
            }
            while (!request.isDone)
            {
                yield return(Timing.WaitForOneFrame);
            }
            if (null == request.asset)
            {
                LogHelper.PrintError($"[ResourceMgr]LoadAsync load asset:{path} failure.");
            }

            //先等一帧;
            yield return(Timing.WaitForOneFrame);

            if (proxy != null)
            {
                proxy.OnFinish(request.asset);
            }
            else
            {
                LogHelper.PrintError($"[ResourceMgr]LoadAsync proxy is null:{path}.");
            }
        }
Exemplo n.º 7
0
        public void Update(float interval)
        {
            if (Enable && Entity != null && (Root.Reslut == BehaviorState.Reset || Root.Reslut == BehaviorState.Running))
            {
                BehaviorState reslut = Root.Behave(Entity, interval);
                switch (reslut)
                {
                case BehaviorState.Reset:
                    break;

                case BehaviorState.Failure:
                    break;

                case BehaviorState.Running:
                    break;

                case BehaviorState.Success:
                    break;

                case BehaviorState.Finish:
                    break;

                default:
                    Enable = false;
                    LogHelper.PrintError("[BehaviorTree]error state.");
                    break;
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// 同步从AssetBundle加载资源;
        /// </summary>
        /// <param name="path"></param>
        /// <param name="isUsePool"></param>
        /// <returns></returns>
        private AssetBundleAssetProxy LoadAssetSync(string path, bool isUsePool)
        {
            path = $"Assets/Bundles/{path}";

            var proxy = PoolMgr.singleton.GetCsharpObject <AssetBundleAssetProxy>();

            proxy.Initialize(path, isUsePool);

            UnityObject asset       = null;
            var         assetBundle = AssetBundleMgr.singleton.LoadFromFile(path);

            if (assetBundle != null)
            {
                var name = Path.GetFileNameWithoutExtension(path);
                asset = assetBundle.LoadAsset(name);
            }
            if (asset == null)
            {
                AssetBundleMgr.singleton.UnloadAsset(path, null);
                LogHelper.PrintError($"[ResourceMgr]LoadSyncAssetProxy load asset:{path} failure.");
            }
            else
            {
                AssetBundleMgr.singleton.AddAssetRef(path, asset);
            }
            proxy.OnFinish(asset);
            return(proxy);
        }
Exemplo n.º 9
0
        protected override void UpdateEx(float interval)
        {
            if (Reslut == BehaviorState.Running)
            {
                bool finish = true;
                for (int i = 0; i < _list.Count; i++)
                {
                    switch (_list[i].Behave(Entity, interval))
                    {
                    case BehaviorState.Running:
                    case BehaviorState.Finish:
                        finish = false;
                        break;

                    case BehaviorState.Success:
                        break;

                    case BehaviorState.Failure:
                    case BehaviorState.Reset:
                        Reslut = BehaviorState.Failure;
                        LogHelper.PrintError("[BTParallel]BTParallel execute failure!");
                        return;

                    default:
                        Reslut = BehaviorState.Failure;
                        LogHelper.PrintError("[BTParallel]BTParallel execute failure!");
                        return;
                    }
                }
                if (finish)
                {
                    Reslut = BehaviorState.Finish;
                }
            }
        }
Exemplo n.º 10
0
        protected override void UpdateEx(float interval)
        {
            if (Reslut == BehaviorState.Running)
            {
                for (int i = 0; i < _list.Count; i++)
                {
                    switch (_list[i].Behave(Entity, interval))
                    {
                    case BehaviorState.Running:
                    case BehaviorState.Finish:
                        Reslut = BehaviorState.Running;
                        return;

                    case BehaviorState.Success:
                        Reslut = BehaviorState.Finish;
                        return;

                    case BehaviorState.Failure:
                    case BehaviorState.Reset:
                        LogHelper.PrintError("[BTSelector]BTSelector execute failure!");
                        break;

                    default:
                        LogHelper.PrintError("[BTSelector]BTSelector execute failure!");
                        break;
                    }
                }
                Reslut = BehaviorState.Failure;
            }
        }
        /// <summary>
        /// 解压Zip包;
        /// </summary>
        /// <param name="filePathName">Zip包的文件路径名</param>
        /// <param name="outputPath">解压输出路径</param>
        /// <param name="password">解压密码</param>
        /// <param name="unzipCallback">UnzipCallback对象,负责回调</param>
        /// <returns></returns>
        public static bool UnzipFile(string filePathName, string outputPath,
                                     string password = null, UnzipCallback unzipCallback = null)
        {
            if (string.IsNullOrEmpty(filePathName) || string.IsNullOrEmpty(outputPath))
            {
                if (null != unzipCallback)
                {
                    unzipCallback.OnFinished(false);
                }

                return(false);
            }

            try
            {
                return(UnzipFile(File.OpenRead(filePathName), outputPath, password, unzipCallback));
            }
            catch (System.Exception e)
            {
                LogHelper.PrintError($"[SharpZipLibHelper.UnzipFile]: {e.ToString()}");

                if (null != unzipCallback)
                {
                    unzipCallback.OnFinished(false);
                }

                return(false);
            }
        }
 public void UnloadUnitySceneMemory(AssetType assetType, Object asset)
 {
     if (asset != null)
     {
         if (assetType == AssetType.Prefab)
         {
             //资源是GameObject;
             GameObject go = asset as GameObject;
             if (go)
             {
                 Destroy(go);
                 return;
             }
             //泛型加载,资源是Prefab上的MonoBehaviour脚本;
             MonoBehaviour monoBehaviour = (MonoBehaviour)asset;
             if (monoBehaviour != null)
             {
                 Destroy(monoBehaviour.gameObject);
                 return;
             }
         }
         if (assetType == AssetType.AnimeClip || assetType == AssetType.AnimeCtrl ||
             assetType == AssetType.Audio || assetType == AssetType.Texture ||
             assetType == AssetType.Material)
         {
             Destroy(asset);
             return;
         }
         LogHelper.PrintError(string.Format("[ResourceMgr]UnloadUnityObject error,AssetType:{0},Object:{1}"
                                            , assetType.ToString(), asset.name));
     }
 }
Exemplo n.º 13
0
        /// <summary>
        /// 广播事件;
        /// </summary>
        /// <param name="receiver">接收者</param>
        /// <param name="type">事件类型</param>
        /// <param name="eventArgs">事件参数</param>
        public void FireEvent(ObjectEx receiver, EventType type, IEventArgs eventArgs)
        {
            if (null == receiver)
            {
                LogHelper.PrintError("[EventMgr]FireEvent error,the receiver is null.");
                return;
            }
            Dictionary <EventType, List <EventHandler> > dict;
            List <EventHandler> list;

            if (EventDict.TryGetValue(receiver, out dict))
            {
                if (null != dict && dict.Count > 0)
                {
                    if (dict.TryGetValue(type, out list))
                    {
                        if (null != list && list.Count > 0)
                        {
                            foreach (var callback in list)
                            {
                                if (callback != null)
                                {
                                    callback(eventArgs);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 14
0
 public void SetSliderValue(float value)
 {
     if (null == CurSlider)
     {
         LogHelper.PrintError("[LuaComponent]Slider is null.");
     }
     CurSlider.value = value;
 }
Exemplo n.º 15
0
 public void PrintException()
 {
     LogHelper.PrintError($"[GameException]{message}");
     if (innerException != null)
     {
         LogHelper.PrintError($"[GameException]{innerException.Message.ToString()}");
     }
 }
 public void PrintException()
 {
     LogHelper.PrintError("[GameException]" + message);
     if (innerException != null)
     {
         LogHelper.PrintError("[GameException]" + innerException.Message.ToString());
     }
 }
Exemplo n.º 17
0
 public void SetSliderValue(float value)
 {
     if (null == Slider)
     {
         LogHelper.PrintError("[LuaUICom]Slider is null.");
         return;
     }
     Slider.value = value;
 }
Exemplo n.º 18
0
 public void SetGray(bool gray)
 {
     if (null == Image)
     {
         LogHelper.PrintError("[LuaUICom]Image is null.");
         return;
     }
     Image.color = gray ? Color.black : Color.white;
 }
Exemplo n.º 19
0
 public void AddClick(UnityAction callBack)
 {
     if (null == CurButton)
     {
         LogHelper.PrintError("[LuaComponent]Button is null.");
     }
     CurButton.onClick.RemoveAllListeners();
     CurButton.onClick.AddListener(callBack);
 }
Exemplo n.º 20
0
        public BTLevel(Hashtable table) : base(table)
        {
            string str = table["Level"].ToString();

            if (!int.TryParse(str, out _level))
            {
                LogHelper.PrintError("[BTLevel]get level is error!");
            }
        }
Exemplo n.º 21
0
 public void SetSprite(string atlas, string icon)
 {
     if (null == CurImage)
     {
         LogHelper.PrintError("[LuaComponent]Image is null.");
     }
     //CurImage.sprite =
     CurImage.SetNativeSize();
 }
Exemplo n.º 22
0
 public void SetSprite(string atlas, string icon)
 {
     if (null == Image)
     {
         LogHelper.PrintError("[LuaUICom]Image is null.");
         return;
     }
     //Image.sprite =
     Image.SetNativeSize();
 }
Exemplo n.º 23
0
 public void AddClick(UnityAction callBack)
 {
     if (null == Button)
     {
         LogHelper.PrintError("[LuaUICom]Button is null.");
         return;
     }
     Button.onClick.RemoveAllListeners();
     Button.onClick.AddListener(callBack);
 }
        /// <summary>
        /// 压缩文件;
        /// </summary>
        /// <param name="filePathName">文件路径名</param>
        /// <param name="parentRelPath">要压缩的文件的父相对文件夹</param>
        /// <param name="zipOutputStream">压缩输出流</param>
        /// <param name="zipCallback">ZipCallback对象,负责回调</param>
        /// <returns></returns>
        private static bool ZipFile(string filePathName, string parentRelPath,
                                    ZipOutputStream zipOutputStream, ZipCallback zipCallback = null)
        {
            //Crc32 crc32 = new Crc32();
            ZipEntry   entry      = null;
            FileStream fileStream = null;

            try
            {
                var entryName = parentRelPath + '/' + Path.GetFileName(filePathName);
                entry = new ZipEntry(entryName)
                {
                    DateTime = System.DateTime.Now
                };

                if ((null != zipCallback) && !zipCallback.OnPreZip(entry))
                {
                    return(true);    // 过滤;
                }

                fileStream = File.OpenRead(filePathName);
                var buffer = new byte[fileStream.Length];
                fileStream.Read(buffer, 0, buffer.Length);
                fileStream.Close();

                entry.Size = buffer.Length;

                //crc32.Reset();
                //crc32.Update(buffer);
                //entry.Crc = crc32.Value;

                zipOutputStream.PutNextEntry(entry);
                zipOutputStream.Write(buffer, 0, buffer.Length);
            }
            catch (System.Exception e)
            {
                LogHelper.PrintError($"[SharpZipLibHelper.ZipFile]: {e.ToString()}");
                return(false);
            }
            finally
            {
                if (null != fileStream)
                {
                    fileStream.Close();
                    fileStream.Dispose();
                }
            }

            if (null != zipCallback)
            {
                zipCallback.OnPostZip(entry);
            }

            return(true);
        }
Exemplo n.º 25
0
 public void PrintException(StackTrace stackTrace = null)
 {
     if (stackTrace != null)
     {
         LogHelper.PrintError($"[{_name}]{_messageBuilder.ToString()}\r\n{stackTrace.ToString()}");
     }
     else
     {
         LogHelper.PrintError($"[{_name}]{_messageBuilder.ToString()}");
     }
 }
Exemplo n.º 26
0
 public static void Send2Lua(int id, byte[] bytes)
 {
     try
     {
         LuaByteBuffer byteBuffer = new LuaByteBuffer(bytes);
         LuaUtility.CallLuaModuleMethod("Protol.ProtoProcess", "Process", id, byteBuffer);
     }
     catch (Exception e)
     {
         LogHelper.PrintError(string.Format("[LuaNetUtility]Send2Lua error,id:{0},info:{1}.", id, e.ToString()));
     }
 }
Exemplo n.º 27
0
 /// <summary>
 /// 创建Component;
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="owner"></param>
 /// <returns></returns>
 public T CreateComponent <T>(ObjectEx owner) where T : AbsComponent, new()
 {
     if (null != owner && owner.AddComponent <T>())
     {
         return(owner.GetComponent <T>());
     }
     else
     {
         LogHelper.PrintError("[ComponentMgr]CreateComponent " + typeof(T).ToString() + " error!");
         return(null);
     }
 }
Exemplo n.º 28
0
 public void Send <T>(T packet) where T : Packet
 {
     try
     {
         _session.Send <T>(packet);
     }
     catch (Exception e)
     {
         LogHelper.PrintError(string.Format("[NetMgr]Send {0} error!", e.ToString()));
         _session.Dispose();
     }
 }
Exemplo n.º 29
0
 public void Send(int id, LuaBuffer buffer)
 {
     try
     {
         _session.Send(id, buffer);
     }
     catch (Exception e)
     {
         LogHelper.PrintError(string.Format("[NetMgr]Send {0} error!", e.ToString()));
         _session.Dispose();
     }
 }
Exemplo n.º 30
0
        public bool InitScrollRect()
        {
            if (_prefab == null)
            {
                LogHelper.PrintError("[LoopScrollRect]prefab is empty!");
                return(false);
            }
            if (_directionCount <= 0)
            {
                _directionCount = 1;
            }
            if (_items.Count > 0)
            {
                LogHelper.PrintError("[LoopScrollRect]Init repeated!");
            }
            if (_scrollRect == null)
            {
                LogHelper.PrintError("[LoopScrollRect]scrollRect is empty!");
                return(false);
            }
            _scrollTrans = _scrollRect.transform.GetComponent <RectTransform>();
            _size        = _scrollTrans.rect.size;
            //设置Mask;
            Mask mask = transform.GetComponentInParent <Mask>();

            if (mask != null)
            {
                RectTransform rect = mask.transform.GetComponent <RectTransform>();
                rect.pivot            = new Vector2(0, 1);
                rect.anchorMin        = new Vector2(0f, 1f);
                rect.anchorMax        = new Vector2(0f, 1f);
                rect.sizeDelta        = _size;
                rect.anchoredPosition = new Vector3(0, 0, 0);
            }
            _rectTrans                  = transform.GetComponent <RectTransform>();
            _rectTrans.pivot            = new Vector2(0, 1);
            _rectTrans.anchorMin        = new Vector2(0f, 1f);
            _rectTrans.anchorMax        = new Vector2(0f, 1f);
            _rectTrans.sizeDelta        = _size;
            _rectTrans.anchoredPosition = new Vector3(0, 0, 0);
            if (_direction == LoopDirection.Horizontal)
            {
                _needCount = (int)Mathf.Ceil(_size.x / _itemSize.x + 1) * _directionCount;
            }
            else if (_direction == LoopDirection.Vertical)
            {
                _needCount = (int)Mathf.Ceil(_size.y / _itemSize.y + 1) * _directionCount;
            }
            _scrollRect.onValueChanged.AddListener(delegate { UpdateLoop(false); });
            _count = 0;
            _items.Clear();
            return(true);
        }