Beispiel #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);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 设置Live2D对象
        /// </summary>
        /// <param name="live2DData"></param>
        public void SetLive2DData(Live2DData live2DData)
        {
            if (this.live2DData != null)
            {
                Clear();
            }

            this.live2DData = live2DData;
            gameObject      = live2DData.GetGameObject();
            transform       = gameObject.transform;

            animator = gameObject.GetComponent <Animator>();

            fadeController = gameObject.GetComponent <CubismFadeController>();
            if (fadeController == null)
            {
                fadeController = gameObject.AddComponent <CubismFadeController>();
            }
            fadeController.CubismFadeMotionList = live2DData.fadeMotionList;

            motionController = gameObject.GetComponent <CubismMotionController>();
            if (motionController == null)
            {
                motionController = gameObject.AddComponent <CubismMotionController>();
            }
        }
Beispiel #3
0
 /// <summary>
 /// 清理
 /// </summary>
 public void Clear()
 {
     transform = null;
     animator  = null;
     fadeController.CubismFadeMotionList = null;
     fadeController   = null;
     motionController = null;
     live2DData.ReleaseGameObject(gameObject);
     live2DData = null;
 }