Exemplo n.º 1
0
        public void LoadRobot(LitJson.JsonData jsonData, Action <IRobot> onCompleted)
        {
            if (jsonData == null)
            {
                Misc.SafeInvoke(onCompleted, null);
                return;
            }
            IRobot robot     = null;
            bool   hideLines = Application.isPlaying;

            try
            {
                ProfilingUtility.BeginSample("RobotFactory.LoadRobot");
                DefaultRobotSerializer defaultRobotSerializer = new DefaultRobotSerializer();
                var dataModel = defaultRobotSerializer.FromJson(jsonData);
                if (dataModel != null)
                {
                    ProfilingUtility.BeginSample("RobotFactory.LoadRobot.PrepareParts");
                    var parts = new Dictionary <string, Queue <GameObject> >(dataModel.assetDatas.Count);
                    foreach (var item in dataModel.assetDatas)
                    {
                        string resID = item.Key;
                        // 去掉线
                        if (hideLines && resID.StartsWith("W") && resID.IndexOf('_') > 0)
                        {
                            continue;
                        }

                        var count = item.Value;
                        while (count > 0)
                        {
                            var part = UbtrobotSettings.GetOrLoad().partsLibrary.Instantiate(resID);
                            if (part == null)
                            {
                                DebugUtility.LogError(LoggerTags.Project, "Missing part : {0}", resID);
                                break;
                            }

                            if (hideLines && resID == "Battery")
                            {
                                part.transform.SetActive("Battery_02", false);
                            }

                            if (!parts.TryGetValue(resID, out var list))
                            {
                                list         = new Queue <GameObject>();
                                parts[resID] = list;
                            }
                            count--;
                            list.Enqueue(part.gameObject);
                        }
                    }
                    ProfilingUtility.EndSample();
                    ProfilingUtility.BeginSample("RobotFactory.LoadRobot.Rebuild");
                    var robotTransform = dataModel.Rebuild(null, parts);
                    ProfilingUtility.EndSample();
                    robot = robotTransform != null?robotTransform.GetComponent <IRobot>() : null;

                    if (robot != null)
                    {
                        if (Application.isPlaying)
                        {
                            RobotManager.GetOrAlloc().AddRobot(robot);
                        }
                        else
                        {
                            robot.AutoInitialize();
                        }
                    }
                }
                ProfilingUtility.EndSample();
            }
            catch (Exception ex)
            {
                DebugUtility.LogException(ex);
            }
            Misc.SafeInvoke(onCompleted, robot);
        }