Skip to content

Instantly share code, notes, and snippets.

@Nobuyuki-Kobayashi
Last active February 22, 2024 02:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Nobuyuki-Kobayashi/7c67cd9b68a06b5f5dd0eda589a1d905 to your computer and use it in GitHub Desktop.
Save Nobuyuki-Kobayashi/7c67cd9b68a06b5f5dd0eda589a1d905 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace UnityChan
{
public class CopyMaterialParameter : EditorWindow
{
[SerializeField]
static Material source;
Material target;
static EditorWindow window;
bool _RemovedUnusedParameter = false;
[MenuItem("CONTEXT/Material/Copy Material Parameter")]
static void Init (MenuCommand command)
{
source = (Material) command.context;
window = EditorWindow.GetWindow<CopyMaterialParameter> (true, "Copy Material Parameter : Select Materials", true);
window.Show ();
}
void OnGUI ()
{
source = (Material)EditorGUILayout.ObjectField ("Source material", source, typeof(Material),true);
target = (Material)EditorGUILayout.ObjectField ("Target material", target, typeof(Material),true);
EditorGUILayout.Space();
if (GUILayout.Button ("Copy Sorce ⇒ Target")) {
CopyMaterial (source, target);
window.Close();
}
if (GUILayout.Button ("Switch Sorce/Target")) {
_RemovedUnusedParameter = false;
var tmp = target;
target = source;
source = tmp;
}
if (GUILayout.Button ("Remove Unused Properties from Sorce")) {
RemoveUnusedMaterialProperties(source);
_RemovedUnusedParameter = true;
}
if(_RemovedUnusedParameter){
EditorGUILayout.HelpBox("Unused Material Properties are removed.",MessageType.Info);
}
}
void CopyMaterial (Material source, Material target)
{
target.shader = source.shader;
target.CopyPropertiesFromMaterial (source);
}
// http://light11.hatenadiary.com/entry/2018/12/04/224253
void RemoveUnusedMaterialProperties(Material material)
{
var sourceProps = new SerializedObject(material);
sourceProps.Update();
var savedProp = sourceProps.FindProperty("m_SavedProperties");
// Tex Envs
var texProp = savedProp.FindPropertyRelative("m_TexEnvs");
for (int i = texProp.arraySize - 1; i >= 0; i--) {
var propertyName = texProp.GetArrayElementAtIndex(i).FindPropertyRelative("first").stringValue;
if (!material.HasProperty(propertyName)) {
texProp.DeleteArrayElementAtIndex(i);
}
}
// Floats
var floatProp = savedProp.FindPropertyRelative("m_Floats");
for (int i = floatProp.arraySize - 1; i >= 0; i--) {
var propertyName = floatProp.GetArrayElementAtIndex(i).FindPropertyRelative("first").stringValue;
if (!material.HasProperty(propertyName)) {
floatProp.DeleteArrayElementAtIndex(i);
}
}
// Colors
var colorProp = savedProp.FindPropertyRelative("m_Colors");
for (int i = colorProp.arraySize - 1; i >= 0; i--) {
var propertyName = colorProp.GetArrayElementAtIndex(i).FindPropertyRelative("first").stringValue;
if (!material.HasProperty(propertyName)) {
colorProp.DeleteArrayElementAtIndex(i);
}
}
sourceProps.ApplyModifiedProperties();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment