コード例 #1
0
        public void Awake(ModelType type, string name)
        {
            try
            {
                Type = type;
                Name = name;
                URL  = Live2dPathHelper.GetModelConfigPath(type, name);
                TextAsset config = ResourcesHelper.GetAsset <TextAsset>(URL);
                ModelConfig           = JsonMapper.ToObject <Live2dModelConfig>(config.text);
                ModelConfig.AssetPath = Live2dPathHelper.GetAssetPath(type, name);
                string modelPath  = ModelConfig.GetModelPath();
                var    model3Json = CubismModel3Json.LoadAtPath(modelPath, BuiltinLoadAssetAtPath);
                Model = model3Json.ToModel();
                EntityRef.GameObject       = Model.gameObject;
                EntityRef.Transform        = Model.gameObject.transform;
                EntityRef.GameObject.layer = LayerHelper.LayerNameToLayer("Unit");
                GameObjectHelper.Traversal(EntityRef.Transform, (arg) =>
                {
                    arg.gameObject.layer = LayerHelper.LayerNameToLayer("Unit");
                }, true);

                Animations     = new Dictionary <string, AnimationClip>();
                fadeController = EntityRef.GameObject.AddComponent <CubismFadeController>();
                fadeController.CubismFadeMotionList = ScriptableObject.CreateInstance <CubismFadeMotionList>();
                fadeController.CubismFadeMotionList.MotionInstanceIds       = new int[ModelConfig.Motions.Length];
                fadeController.CubismFadeMotionList.CubismFadeMotionObjects = new CubismFadeMotionData[ModelConfig.Motions.Length];

                for (int i = 0; i < ModelConfig.Motions.Length; ++i)
                {
                    string motion      = ModelConfig.Motions[i];
                    string motionPath  = ModelConfig.GetMotionPath(motion);
                    var    motion3Json = CubismMotion3Json.LoadFrom(ResourcesHelper.GetAsset <TextAsset>(motionPath));
                    var    clip        = ResourcesHelper.GetAsset <AnimationClip>(ModelConfig.GetAnimationClipPath(motion));
                    clip.name = motion;

                    int instanceId = clip.GetInstanceID();
                    var events     = clip.events;
                    for (var j = 0; j < events.Length; ++j)
                    {
                        if (events[j].functionName != "InstanceId")
                        {
                            continue;
                        }

                        instanceId = events[j].intParameter;
                    }

                    fadeController.CubismFadeMotionList.MotionInstanceIds[i]       = instanceId;
                    fadeController.CubismFadeMotionList.CubismFadeMotionObjects[i] = CubismFadeMotionData.CreateInstance(motion3Json, clip.name, clip.length);

                    Animations.Add(motion, clip);
                }

                motionController = EntityRef.GameObject.AddComponent <CubismMotionController>();
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }