207 lines
6.4 KiB
C#
207 lines
6.4 KiB
C#
|
//============= Copyright (c) Ludic GmbH, All rights reserved. ==============
|
|||
|
//
|
|||
|
// Purpose: Part of the My Behaviour Tree Controller Code
|
|||
|
//
|
|||
|
//=============================================================================
|
|||
|
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
using MyBT;
|
|||
|
|
|||
|
#if UNITY_EDITOR
|
|||
|
using UnityEditor;
|
|||
|
[CustomEditor(typeof(NamedRenderer))]
|
|||
|
public class NamedRendererInspector : ComponentHandlerInspector {
|
|||
|
}
|
|||
|
#endif
|
|||
|
|
|||
|
[System.Serializable]
|
|||
|
public class NamedRenderer : ComponentHandler {
|
|||
|
public override string TypeLabel () {
|
|||
|
return "Renderer";
|
|||
|
}
|
|||
|
|
|||
|
public override string ContentLabel() {
|
|||
|
UpdateComponent();
|
|||
|
if (rendererComponents.Length > 0) {
|
|||
|
Renderer r = rendererComponents[0];
|
|||
|
MeshFilter mf = rendererComponents[0].GetComponent<MeshFilter>();
|
|||
|
if (useMeshNameOrMaterialNameForName) {
|
|||
|
if (mf != null) {
|
|||
|
return mf.sharedMesh.name;
|
|||
|
}
|
|||
|
if (r != null) {
|
|||
|
return r.sharedMaterial.name;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return "";
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public override void UpdateComponent() {
|
|||
|
base.UpdateComponent();
|
|||
|
rendererComponents = GetComponentsInChildren<Renderer>();
|
|||
|
|
|||
|
changeColorCutoff = hasMaterialCutoff;
|
|||
|
changeColorAlpha = hasMaterialAlpha;
|
|||
|
}
|
|||
|
|
|||
|
public Renderer[] rendererComponents = new Renderer[0];
|
|||
|
|
|||
|
public bool instantiateMaterials = true;
|
|||
|
public bool changeColorCutoff = true;
|
|||
|
public bool changeColorAlpha = true;
|
|||
|
public bool changeRendererActive = true;
|
|||
|
public bool useMeshNameOrMaterialNameForName = true;
|
|||
|
|
|||
|
public override string titleText {
|
|||
|
get {
|
|||
|
return "Show/Hide, FadeIn/Out of Renderer using alpha channel of diffuse color";
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public override string[][] helpText {
|
|||
|
get {
|
|||
|
return new string[][] {
|
|||
|
new string[] {"Show", null, $"BTC.Show(\"{roomId}\", \"{gameObject.name}\")"},
|
|||
|
new string[] {"Hide", null, $"BTC.Hide(\"{roomId}\", \"{gameObject.name}\")"},
|
|||
|
new string[] {"FadeIn", null, $"BTC.FadeIn(\"{roomId}\", \"{gameObject.name}\")"},
|
|||
|
new string[] {"FadeOut", null, $"BTC.FadeOut(\"{roomId}\", \"{gameObject.name}\")"},
|
|||
|
};
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public override void Start () {
|
|||
|
if (instantiateMaterials) {
|
|||
|
if ((rendererComponents.Length == 0) || (rendererComponents[0] == null)) {
|
|||
|
return;
|
|||
|
}
|
|||
|
foreach (Renderer renderer in rendererComponents) {
|
|||
|
foreach (Material mat in renderer.materials) {
|
|||
|
// make color an instance
|
|||
|
if (mat.HasProperty("_Color")) {
|
|||
|
// only when playing, because of leaking materials
|
|||
|
if (Application.isPlaying) {
|
|||
|
mat.SetColor("_Color", mat.color);
|
|||
|
}
|
|||
|
}
|
|||
|
else {
|
|||
|
Debug.LogWarning(gameObject.name + " has no color");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#region both values setting
|
|||
|
public override void SetAlpha(float alpha) {
|
|||
|
if (changeColorCutoff) {
|
|||
|
SetMaterialCutoff(alpha);
|
|||
|
}
|
|||
|
if (changeColorAlpha) {
|
|||
|
SetMaterialAlpha(alpha);
|
|||
|
}
|
|||
|
if (changeRendererActive) {
|
|||
|
SetRendererActive(alpha);
|
|||
|
fakeAlpha = alpha;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public override float GetAlpha() {
|
|||
|
if (changeColorCutoff) {
|
|||
|
return GetMaterialCutoff();
|
|||
|
}
|
|||
|
if (changeColorAlpha) {
|
|||
|
return GetMaterialAlpha();
|
|||
|
}
|
|||
|
return fakeAlpha;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region both values setting
|
|||
|
private Color _prevColor;
|
|||
|
private void SetMaterialAlpha(float alpha) {
|
|||
|
if ((rendererComponents.Length != 0) && (rendererComponents[0] != null)) {
|
|||
|
foreach (Renderer renderer in rendererComponents) {
|
|||
|
foreach (Material mat in renderer.materials) {
|
|||
|
if (mat.HasProperty("_Color")) {
|
|||
|
Color prevColor = mat.color;
|
|||
|
Color newColor = prevColor;
|
|||
|
newColor.a = 1 - alpha;
|
|||
|
if (prevColor != newColor) {
|
|||
|
mat.color = newColor;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private float GetMaterialAlpha() {
|
|||
|
if ((rendererComponents.Length != 0) && (rendererComponents[0] != null)) {
|
|||
|
return 1 - rendererComponents[0].sharedMaterial.color.a;
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
private bool hasMaterialAlpha {
|
|||
|
get {
|
|||
|
if (rendererComponents.Length > 0) {
|
|||
|
return rendererComponents[0].sharedMaterial.HasProperty("_Color");
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region cutoff
|
|||
|
private float _prevCutoff;
|
|||
|
private void SetMaterialCutoff(float cutOffValue) {
|
|||
|
if ((rendererComponents.Length == 0) || (rendererComponents[0] == null)) {
|
|||
|
return;
|
|||
|
}
|
|||
|
if (cutOffValue != _prevCutOff) {
|
|||
|
foreach (Renderer renderer in rendererComponents) {
|
|||
|
foreach (Material mat in renderer.materials) {
|
|||
|
if (mat.HasProperty("_Cutoff")) {
|
|||
|
mat.SetFloat("_Cutoff", cutOffValue);
|
|||
|
_prevCutoff = cutOffValue;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private float GetMaterialCutoff() {
|
|||
|
if ((rendererComponents.Length == 0) || (rendererComponents[0] == null)) {
|
|||
|
return 0;
|
|||
|
}
|
|||
|
if (hasMaterialCutoff) {
|
|||
|
_prevCutOff = rendererComponents[0].sharedMaterial.GetFloat("_Cutoff");
|
|||
|
}
|
|||
|
return _prevCutOff;
|
|||
|
}
|
|||
|
|
|||
|
private bool hasMaterialCutoff {
|
|||
|
get {
|
|||
|
if (rendererComponents.Length > 0) {
|
|||
|
return rendererComponents[0].sharedMaterial.HasProperty("_Cutoff");
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region renderer active inactive
|
|||
|
float fakeAlpha = 0;
|
|||
|
private void SetRendererActive (float alpha) {
|
|||
|
foreach (Renderer renderer in rendererComponents) {
|
|||
|
renderer.enabled = (alpha <= 0);
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
}
|