Skip to content

Commit 72412d4

Browse files
committed
v1.0.0
1 parent 7686465 commit 72412d4

15 files changed

+594
-0
lines changed

Assets/CubismPoser.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/CubismPoser/CubismPoser.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#if UNITY_EDITOR
2+
using Live2D.Cubism.Core;
3+
using Live2D.Cubism.Framework;
4+
using System.Threading;
5+
using UnityEngine;
6+
7+
namespace SF.Cubism.Poser
8+
{
9+
[RequireComponent(typeof(CubismModel))]
10+
[ExecuteInEditMode]
11+
public class CubismPoser : MonoBehaviour
12+
{
13+
private readonly CancellationTokenSource _cts = new();
14+
private CancellationToken _token;
15+
16+
private CubismModel _model;
17+
private CubismUpdateController _updateController;
18+
private bool _initialized;
19+
20+
public CubismModel Model => _model;
21+
public CancellationToken Token => _token;
22+
23+
private void Awake()
24+
{
25+
_token = _cts.Token;
26+
27+
if (!Application.isPlaying)
28+
{
29+
_model = GetComponent<CubismModel>();
30+
_updateController = GetComponent<CubismUpdateController>();
31+
32+
if (TryGetComponent<CubismParametersInspector>(out var cubismInspector))
33+
DestroyImmediate(cubismInspector);
34+
35+
CubismUpdater(false);
36+
_initialized = true;
37+
}
38+
else
39+
Destroy(this);
40+
}
41+
42+
private void Update()
43+
{
44+
if (_model != null)
45+
_model.ForceUpdateNow();
46+
}
47+
48+
private void OnDestroy()
49+
{
50+
_cts.Cancel();
51+
_cts.Dispose();
52+
53+
if (_initialized && gameObject != null)
54+
{
55+
CubismUpdater(true);
56+
gameObject.AddComponent<CubismParametersInspector>();
57+
}
58+
}
59+
60+
private void CubismUpdater(bool state)
61+
{
62+
if (_model != null)
63+
_model.enabled = state;
64+
65+
if (_updateController != null)
66+
_updateController.enabled = state;
67+
}
68+
}
69+
}
70+
#endif

Assets/CubismPoser/CubismPoser.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#if UNITY_EDITOR
2+
using System;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
namespace SF.Cubism.Poser
7+
{
8+
public class CubismPoserGroups : MonoBehaviour
9+
{
10+
public List<CubismPoserGroup> Groups = new();
11+
}
12+
13+
[Serializable]
14+
public class CubismPoserGroup
15+
{
16+
public string Name;
17+
public List<string> Parameters = new();
18+
}
19+
}
20+
#endif

Assets/CubismPoser/CubismPoserGroups.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/CubismPoser/Editor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using UnityEngine;
2+
using System.Reflection;
3+
using System;
4+
using Object = UnityEngine.Object;
5+
6+
namespace SF.Cubism.Poser
7+
{
8+
public static class AnimationWindowPipe
9+
{
10+
#region OpenedAnimationWindow [private helper class]
11+
private class OpenedAnimationWindow
12+
{
13+
public Object Window;
14+
public Type AnimEditorType;
15+
public object AnimEditor;
16+
public Type WindowStateType;
17+
public object WindowStateObject;
18+
}
19+
#endregion
20+
21+
private static readonly BindingFlags s_flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
22+
private static Type s_animationWindowType = null;
23+
24+
public static bool IsRecording() =>
25+
GetWindowProperty<bool>("recording");
26+
27+
private static T GetWindowProperty<T>(string name)
28+
{
29+
var window = GetOpenedAnimationWindow();
30+
31+
if (window != null)
32+
{
33+
var controlInterface = window.WindowStateType.GetProperty("controlInterface").GetValue(window.WindowStateObject);
34+
return (T)controlInterface.GetType().GetProperty(name).GetValue(controlInterface);
35+
}
36+
37+
return default;
38+
}
39+
40+
private static OpenedAnimationWindow GetOpenedAnimationWindow()
41+
{
42+
Object[] openAnimationWindows = Resources.FindObjectsOfTypeAll(GetAnimationWindowType());
43+
44+
if (openAnimationWindows.Length == 0)
45+
return null;
46+
47+
var window = openAnimationWindows[0];
48+
49+
FieldInfo animEditor = GetAnimationWindowType().GetField("m_AnimEditor", s_flags);
50+
51+
Type animEditorType = animEditor.FieldType;
52+
object animEditorObject = animEditor.GetValue(window);
53+
FieldInfo animWindowState = animEditorType.GetField("m_State", s_flags);
54+
Type windowStateType = animWindowState.FieldType;
55+
56+
var openedWindow = new OpenedAnimationWindow()
57+
{
58+
Window = window,
59+
AnimEditorType = animEditorType,
60+
AnimEditor = animEditorObject,
61+
WindowStateType = windowStateType,
62+
WindowStateObject = animWindowState.GetValue(animEditorObject)
63+
};
64+
65+
return openedWindow;
66+
}
67+
68+
private static Type GetAnimationWindowType()
69+
{
70+
s_animationWindowType ??= Type.GetType("UnityEditor.AnimationWindow,UnityEditor");
71+
return s_animationWindowType;
72+
}
73+
}
74+
}

Assets/CubismPoser/Editor/AnimationWindowPipe.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)