Skip to content

Commit f9a1e52

Browse files
Merge pull request Live2D#30 from Live2D/feature/add_canvas_information
add canvas information
2 parents 5523a7b + 590e7df commit f9a1e52

File tree

2 files changed

+149
-1
lines changed

2 files changed

+149
-1
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright(c) Live2D Inc. All rights reserved.
3+
*
4+
* Use of this source code is governed by the Live2D Open Software license
5+
* that can be found at http://live2d.com/eula/live2d-open-software-license-agreement_en.html.
6+
*/
7+
8+
9+
using Live2D.Cubism.Core.Unmanaged;
10+
using Live2D.Cubism.Framework;
11+
using UnityEngine;
12+
13+
14+
namespace Live2D.Cubism.Core
15+
{
16+
/// <summary>
17+
/// Single <see cref="CubismModel"/> canvas information.
18+
/// </summary>
19+
[CubismDontMoveOnReimport]
20+
public sealed class CubismCanvasInformation
21+
{
22+
/// <summary>
23+
/// Initializes instance.
24+
/// </summary>
25+
/// <param name="unmanagedModel">Handle to unmanaged model.</param>
26+
public CubismCanvasInformation(CubismUnmanagedModel unmanagedModel)
27+
{
28+
Reset(unmanagedModel);
29+
}
30+
31+
32+
/// <summary>
33+
/// Unmanaged canvas information from unmanaged model.
34+
/// </summary>
35+
private CubismUnmanagedCanvasInformation UnmanagedCanvasInformation { get; set; }
36+
37+
38+
/// <summary>
39+
/// Width of native model canvas.
40+
/// </summary>
41+
public float CanvasWidth
42+
{
43+
get
44+
{
45+
// Pull data.
46+
return UnmanagedCanvasInformation.CanvasWidth;
47+
}
48+
}
49+
50+
51+
/// <summary>
52+
/// Height of native model canvas.
53+
/// </summary>
54+
public float CanvasHeight
55+
{
56+
get
57+
{
58+
// Pull data.
59+
return UnmanagedCanvasInformation.CanvasHeight;
60+
}
61+
}
62+
63+
64+
/// <summary>
65+
/// Coordinate origin of X axis.
66+
/// </summary>
67+
public float CanvasOriginX
68+
{
69+
get
70+
{
71+
// Pull data.
72+
return UnmanagedCanvasInformation.CanvasOriginX;
73+
}
74+
}
75+
76+
77+
/// <summary>
78+
/// Coordinate origin of Y axis.
79+
/// </summary>
80+
public float CanvasOriginY
81+
{
82+
get
83+
{
84+
// Pull data.
85+
return UnmanagedCanvasInformation.CanvasOriginY;
86+
}
87+
}
88+
89+
90+
/// <summary>
91+
/// Pixels per unit of native model.
92+
/// </summary>
93+
public float PixelsPerUnit
94+
{
95+
get
96+
{
97+
// Pull data.
98+
return UnmanagedCanvasInformation.PixelsPerUnit;
99+
}
100+
}
101+
102+
103+
/// <summary>
104+
/// Revives the instance.
105+
/// </summary>
106+
/// <param name="unmanagedModel">Handle to unmanaged model.</param>
107+
internal void Revive(CubismUnmanagedModel unmanagedModel)
108+
{
109+
UnmanagedCanvasInformation = unmanagedModel.CanvasInformation;
110+
}
111+
112+
/// <summary>
113+
/// Restores instance to initial state.
114+
/// </summary>
115+
/// <param name="unmanagedModel">Handle to unmanaged model.</param>
116+
private void Reset(CubismUnmanagedModel unmanagedModel)
117+
{
118+
Revive(unmanagedModel);
119+
}
120+
}
121+
}

Assets/Live2D/Cubism/Core/CubismModel.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,30 @@ public CubismDrawable[] Drawables
173173
private set { _drawables = value; }
174174
}
175175

176+
/// <summary>
177+
/// <see cref="CanvasInformation"/> backing field.
178+
/// </summary>
179+
[NonSerialized]
180+
private CubismCanvasInformation _canvasInformation;
181+
182+
/// <summary>
183+
/// Canvas information of model.
184+
/// </summary>
185+
public CubismCanvasInformation CanvasInformation
186+
{
187+
get
188+
{
189+
if (_canvasInformation == null)
190+
{
191+
Revive();
192+
}
193+
194+
195+
return _canvasInformation;
196+
}
197+
private set { _canvasInformation = value; }
198+
}
199+
176200
/// <summary>
177201
/// Parameter store cache.
178202
/// </summary>
@@ -222,11 +246,12 @@ private void Revive()
222246
Parts = GetComponentsInChildren<CubismPart>();
223247
Drawables = GetComponentsInChildren<CubismDrawable>();
224248

225-
226249
Parameters.Revive(TaskableModel.UnmanagedModel);
227250
Parts.Revive(TaskableModel.UnmanagedModel);
228251
Drawables.Revive(TaskableModel.UnmanagedModel);
229252

253+
CanvasInformation = new CubismCanvasInformation(TaskableModel.UnmanagedModel);
254+
230255
_parameterStore = GetComponent<CubismParameterStore>();
231256
}
232257

@@ -255,6 +280,8 @@ private void Reset(CubismMoc moc)
255280
Parameters = parameters.GetComponentsInChildren<CubismParameter>();
256281
Parts = parts.GetComponentsInChildren<CubismPart>();
257282
Drawables = drawables.GetComponentsInChildren<CubismDrawable>();
283+
284+
CanvasInformation = new CubismCanvasInformation(TaskableModel.UnmanagedModel);
258285
}
259286

260287
/// <summary>

0 commit comments

Comments
 (0)