PlayerVisual Customization
This commit is contained in:
@@ -3,11 +3,19 @@ using UnityEngine;
|
||||
|
||||
public class PlayerVisual : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
private class Customization
|
||||
{
|
||||
[ColorUsage(false, true)] public Color[] Colors;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
private class References
|
||||
{
|
||||
public Player Player;
|
||||
public Animator Anim;
|
||||
public Renderer[] Renderers;
|
||||
public Material[] Materials;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
@@ -19,15 +27,20 @@ public class PlayerVisual : MonoBehaviour
|
||||
public string Trigger;
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
private Customization _customization;
|
||||
|
||||
[SerializeField]
|
||||
private References _references;
|
||||
|
||||
[SerializeField]
|
||||
private PlayerAnimationStateMapper[] _playerAnimationStateMapper;
|
||||
|
||||
private const string PP_CUSTOM = "CustomPlayer";
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
LoadCustom();
|
||||
}
|
||||
|
||||
void Update()
|
||||
@@ -35,8 +48,54 @@ public class PlayerVisual : MonoBehaviour
|
||||
UpdateAnimation();
|
||||
}
|
||||
|
||||
public void RandomizeCustom()
|
||||
{
|
||||
Debug.Log("randomize");
|
||||
}
|
||||
|
||||
private void SetCustom()
|
||||
{
|
||||
foreach (Renderer rend in _references.Renderers)
|
||||
{
|
||||
foreach (Material mat in rend.materials)
|
||||
{
|
||||
Material sourceMat = _references.Materials.FirstOrDefault(m => mat.name.StartsWith(m.name));
|
||||
|
||||
if (sourceMat)
|
||||
{
|
||||
Color col = _customization.Colors[System.Array.IndexOf(_references.Materials, sourceMat)];
|
||||
|
||||
if (!mat.IsKeywordEnabled("_EMISSION"))
|
||||
mat.color = col;
|
||||
else
|
||||
mat.SetColor("_EmissionColor", col);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadCustom()
|
||||
{
|
||||
if (!PlayerPrefs.HasKey(PP_CUSTOM))
|
||||
return;
|
||||
|
||||
string json = PlayerPrefs.GetString(PP_CUSTOM);
|
||||
_customization = JsonUtility.FromJson<Customization>(json);
|
||||
|
||||
SetCustom();
|
||||
}
|
||||
|
||||
private void SaveCustom()
|
||||
{
|
||||
string json = JsonUtility.ToJson(_customization);
|
||||
PlayerPrefs.SetString(PP_CUSTOM, json);
|
||||
}
|
||||
|
||||
private void UpdateAnimation()
|
||||
{
|
||||
if (!_references.Player)
|
||||
return;
|
||||
|
||||
if (_references.Anim.IsInTransition(0))
|
||||
return;
|
||||
|
||||
@@ -61,4 +120,17 @@ public class PlayerVisual : MonoBehaviour
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static Color ShiftHue(Color color, float hueShift)
|
||||
{
|
||||
Color.RGBToHSV(color, out float h, out float s, out float v);
|
||||
|
||||
h += hueShift / 360f;
|
||||
h = Mathf.Repeat(h, 1f);
|
||||
|
||||
Color result = Color.HSVToRGB(h, s, v);
|
||||
result.a = color.a;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user