Skip to content

Commit

Permalink
✨ feat: Renderer Utility
Browse files Browse the repository at this point in the history
  • Loading branch information
esnya committed Jun 10, 2021
1 parent 929910c commit 94fe26a
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 0 deletions.
130 changes: 130 additions & 0 deletions Assets/EsnyaUnityTools/Editor/RendererUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace EsnyaFactory
{
public enum RendererUtilityTool
{
MaterialReplace,
AnchorOverride,
}

public class RendererUtility : EditorWindow
{

public RendererUtilityTool tool;

public GameObject rootObject;
public bool includeChlidren = true;
public bool includeInactive = true;

public Transform anchorOverride;

public Material materialReplaceOnly;
public Material materialReplaceBy;

private Vector2 scrollPosition;


[MenuItem("EsnyaTools/Renderer Utility")]
private static void ShowWindow()
{
var window = GetWindow<RendererUtility>();
window.Show();
}

private void Replace(Renderer renderer)
{
Undo.RecordObject(renderer, "Replace");
switch (tool)
{
case RendererUtilityTool.MaterialReplace:
renderer.sharedMaterials = renderer.sharedMaterials.Select(m => {
return (materialReplaceOnly == null || m != materialReplaceOnly) ? m : materialReplaceBy;
}).ToArray();
break;
case RendererUtilityTool.AnchorOverride:
renderer.probeAnchor = anchorOverride;
break;
}
}

private bool RendererFilter(Renderer renderer)
{
if (includeInactive && !renderer.gameObject.activeInHierarchy) return false;

switch (tool)
{
case RendererUtilityTool.MaterialReplace:
return materialReplaceOnly == null || renderer.sharedMaterials.Contains(materialReplaceOnly);
case RendererUtilityTool.AnchorOverride:
return renderer.probeAnchor != anchorOverride;
default:
return true;
}
}

private void OnEnable()
{
titleContent = new GUIContent("Renderer Utility");
}

private void OnGUI()
{
EditorGUILayout.Space();

EEUI.Toolbar(ref tool);

EEUI.Scroll(ref scrollPosition, () => {
EditorGUILayout.Space();
EEUI.ObjectFieldWithSelection("Root Object", ref rootObject, true);
if (rootObject == null) return;
EEUI.ValueField("Include Children", ref includeChlidren);
if (includeChlidren) EEUI.ValueField("Include Inactive", ref includeInactive);
var renderers = includeChlidren
? rootObject.GetComponentsInChildren<Renderer>().Where(RendererFilter)
: Enumerable.Repeat(rootObject.GetComponent<Renderer>(), 1).Where(r => r != null);
switch (tool)
{
case RendererUtilityTool.MaterialReplace:
if (includeChlidren) EEUI.ObjectFieldWithSelection("Replace Only", ref materialReplaceOnly, true);
break;
}
EditorGUILayout.Space();
switch (tool)
{
case RendererUtilityTool.MaterialReplace:
EEUI.ObjectFieldWithSelection("Replace By", ref materialReplaceBy, true);
break;
case RendererUtilityTool.AnchorOverride:
EditorGUILayout.Space();
EEUI.ObjectFieldWithSelection("Anchor Override", ref anchorOverride, true);
break;
}
EditorGUILayout.Space();
EEUI.Button("Replace All", () => {
foreach (var renderer in renderers) Replace(renderer);
});
EditorGUILayout.Space();
EEUI.BoldLabel("Renderers");
foreach (var renderer in renderers)
{
var r = renderer;
EEUI.ObjectFieldWithAction(renderer.gameObject.name, ref r, true, "Replace", () => Replace(r));
}
});
}
}
}
11 changes: 11 additions & 0 deletions Assets/EsnyaUnityTools/Editor/RendererUtility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/EsnyaUnityTools/Editor/UI.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions Assets/EsnyaUnityTools/Editor/UI/EsnyaEnhancedUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System;
using Ludiq.OdinSerializer.Utilities;
using System.Linq;

namespace EsnyaFactory
{
public class EEUI
{
public static readonly GUILayoutOption[] minIButtonLayout = {
GUILayout.ExpandWidth(false),
// GUILayout.Width(100),
};

public static void Scroll(ref Vector2 scrollPosition, System.Action GUIRenderer)
{
using (var scope = new EditorGUILayout.ScrollViewScope(scrollPosition))
{
scrollPosition = scope.scrollPosition;
GUIRenderer();
}
}


public static void BoldLabel(string label)
{
EditorGUILayout.LabelField(label, EditorStyles.boldLabel);
}

public static void ValueField(string label, ref bool value)
{
value = EditorGUILayout.Toggle(label, value);
}

public static void ValueField<T>(string label, ref T value) where T : Enum
{
value = (T)EditorGUILayout.EnumPopup(label, value);
}

public static void ObjectField<T>(string label, ref T value, bool allowSceneObjects) where T : UnityEngine.Object
{
value = EditorGUILayout.ObjectField(label, value, typeof(T), allowSceneObjects) as T;
}

public static void ObjectFieldWithAction<T>(string label, ref T value, bool allowSceneObjects, string actionLabel, Func<T, T> Action) where T : UnityEngine.Object
{
using (new EditorGUILayout.HorizontalScope())
{
ObjectField(label, ref value, allowSceneObjects);
if (GUILayout.Button(actionLabel, EditorStyles.miniButton, minIButtonLayout)) value = Action(value);
}
}
public static void ObjectFieldWithAction<T>(string label, ref T value, bool allowSceneObjects, string actionLabel, Action Action) where T : UnityEngine.Object
{
ObjectFieldWithAction(label, ref value, allowSceneObjects, actionLabel, v => { Action(); return v; });
}

public static void ObjectFieldWithSelection(string label, ref GameObject value, bool allowSceneObjects)
{
ObjectFieldWithAction(label, ref value, allowSceneObjects, "From Selected", (v) => Selection.activeGameObject ?? v);
}
public static void ObjectFieldWithSelection<T>(string label, ref T value, bool allowSceneObjects) where T : UnityEngine.Object
{
ObjectFieldWithAction(
label,
ref value,
allowSceneObjects,
"From Selected",
(v) => (typeof(T).InheritsFrom<Component>() ? Selection.activeGameObject?.GetComponent<T>() : Selection.activeObject as T) ?? v
);
}

public static void Button(string label, Action Action)
{
if (GUILayout.Button(label)) Action();
}

public static void Toolbar<T>(ref T value) where T : Enum
{
var type = value.GetType();
var names = Enum.GetNames(type);
var values = Enum.GetValues(type) as T[];
var value_ = value;
var currentSelected = values.Select((v, i) => (v, i)).FirstOrDefault(t => t.v.Equals(value_)).i;
var selected = GUILayout.Toolbar(currentSelected, names);
if (selected != currentSelected) value = values[selected];
}
}
}
11 changes: 11 additions & 0 deletions Assets/EsnyaUnityTools/Editor/UI/EsnyaEnhancedUI.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 94fe26a

Please sign in to comment.