Skip to content

Commit 70d35ee

Browse files
committed
fix: Get material from Unity Object
1 parent a95ca6b commit 70d35ee

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

Assets/JCSUnity/Scripts/Effects/Tweener/JCS_ShaderTweener.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void SetValue(float val)
5353
{
5454
foreach (string prop in mShaderProps)
5555
{
56-
this.mRenderer.material.SetFloat(prop, val);
56+
this.LocalMaterial.SetFloat(prop, val);
5757
}
5858
}
5959

@@ -64,7 +64,7 @@ private float GetValue()
6464
{
6565
foreach (string prop in mShaderProps)
6666
{
67-
return this.mRenderer.material.GetFloat(prop);
67+
return this.LocalMaterial.GetFloat(prop);
6868
}
6969

7070
return 0.0f;

Assets/JCSUnity/Scripts/JCS_UnityObject.cs

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public class JCS_UnityObject : MonoBehaviour
2929
{
3030
/* Variables */
3131

32-
private Material mMaterial = null;
33-
3432
[Separator("Initialize Variables (JCS_Unityobject)")]
3533

3634
[Tooltip("Type of the Unity game object.")]
@@ -69,7 +67,6 @@ public void SetObjectType(JCS_UnityObjectType ob)
6967
UpdateUnityData();
7068
}
7169
public JCS_UnityObjectType GetObjectType() { return this.mObjectType; }
72-
public Material GetMaterial() { return this.mMaterial; }
7370
public Image GetImage() { return this.mImage; }
7471
public Renderer GetRenderer() { return this.mRenderer; }
7572
public SpriteRenderer GetSpriteRenderer() { return this.mSpriteRenderer; }
@@ -131,36 +128,31 @@ public virtual void UpdateMaterial(bool force = false)
131128
{
132129
case JCS_UnityObjectType.GAME_OBJECT:
133130
{
134-
mMaterial = Instantiate(mRenderer.material);
135-
mRenderer.material = mMaterial;
131+
mRenderer.material = Instantiate(mRenderer.material);
136132
}
137133
break;
138134
case JCS_UnityObjectType.UI:
139135
{
140136
if (mImage != null)
141137
{
142-
mMaterial = Instantiate(mImage.material);
143-
mImage.material = mMaterial;
138+
mImage.material = Instantiate(mImage.material);
144139
}
145140
}
146141
break;
147142
case JCS_UnityObjectType.SPRITE:
148143
{
149-
mMaterial = Instantiate(mSpriteRenderer.material);
150-
mSpriteRenderer.material = mMaterial;
144+
mSpriteRenderer.material = Instantiate(mSpriteRenderer.material);
151145
}
152146
break;
153147
case JCS_UnityObjectType.TEXT:
154148
{
155-
mMaterial = Instantiate(mText.material);
156-
mText.material = mMaterial;
149+
mText.material = Instantiate(mText.material);
157150
}
158151
break;
159152
#if TMP_PRO
160153
case JCS_UnityObjectType.TMP:
161154
{
162-
mMaterial = Instantiate(mTextMesh.material);
163-
mTextMesh.material = mMaterial;
155+
mTextMesh.material = Instantiate(mTextMesh.material);
164156
}
165157
break;
166158
#endif
@@ -237,7 +229,7 @@ public Component LocalType
237229
}
238230

239231
/// <summary>
240-
/// Get Current type's transform
232+
/// Get Current type's transform.
241233
/// </summary>
242234
public Transform LocalTransform
243235
{
@@ -263,6 +255,57 @@ public Transform LocalTransform
263255
}
264256
}
265257

258+
/// <summary>
259+
/// Return the current type's material.
260+
/// </summary>
261+
public Material LocalMaterial
262+
{
263+
get
264+
{
265+
switch (GetObjectType())
266+
{
267+
case JCS_UnityObjectType.GAME_OBJECT:
268+
return this.mRenderer.material;
269+
case JCS_UnityObjectType.SPRITE:
270+
return this.mSpriteRenderer.material;
271+
case JCS_UnityObjectType.UI:
272+
return this.mImage.material;
273+
case JCS_UnityObjectType.TEXT:
274+
return this.mText.material;
275+
#if TMP_PRO
276+
case JCS_UnityObjectType.TMP:
277+
return this.mTextMesh.material;
278+
#endif
279+
}
280+
281+
return null;
282+
}
283+
284+
set
285+
{
286+
switch (GetObjectType())
287+
{
288+
case JCS_UnityObjectType.GAME_OBJECT:
289+
this.mRenderer.material = value;
290+
break;
291+
case JCS_UnityObjectType.SPRITE:
292+
this.mSpriteRenderer.material = value;
293+
break;
294+
case JCS_UnityObjectType.UI:
295+
this.mImage.material = value;
296+
break;
297+
case JCS_UnityObjectType.TEXT:
298+
this.mText.material = value;
299+
break;
300+
#if TMP_PRO
301+
case JCS_UnityObjectType.TMP:
302+
this.mTextMesh.material = value;
303+
break;
304+
#endif
305+
}
306+
}
307+
}
308+
266309
/// <summary>
267310
/// Get Current type's position
268311
/// </summary>
@@ -566,21 +609,6 @@ public bool LocalEnabled
566609
}
567610
}
568611

569-
/// <summary>
570-
/// Return the possible material.
571-
/// </summary>
572-
public Material LocalMaterial
573-
{
574-
get
575-
{
576-
if (mMaterial == null)
577-
UpdateMaterial();
578-
579-
return this.mMaterial;
580-
}
581-
set { this.mMaterial = value; }
582-
}
583-
584612
/// <summary>
585613
/// Return the possible material color.
586614
/// </summary>

docs/ScriptReference/JCS_UnityObject.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ from its component.
1717
| mColorProps | Shader's color properties. |
1818
| LocalType | Component of this current type. |
1919
| LocalTransform | Either regular transform or recttransform base on the type. |
20+
| LocalMaterial | Local material of this gameobject. |
2021
| Position | Position of this gameobject. |
2122
| LocalPosition | Local position of this gameobject. |
2223
| EulerAngles | Euler angles of this gameobject. |
2324
| LocalEulerAngles | Local euler angles of this gameobject. |
2425
| LocalScale | Local scale of this gameobject. |
2526
| LocalEnabled | Enabled/disable this gameobject. |
26-
| LocalMaterial | Local material of this gameobject. |
2727
| LocalColor | Color of this gameobject. |
2828
| LocalAlpha | Alpha channel of this gameobject. |
2929
| LocalRed | Red channel of this gameobject. |

0 commit comments

Comments
 (0)