Skip to content

Instantly share code, notes, and snippets.

@tm8r
Created May 2, 2016 10:58
Show Gist options
  • Save tm8r/b8f046b21070c0de968adb00e33a6395 to your computer and use it in GitHub Desktop.
Save tm8r/b8f046b21070c0de968adb00e33a6395 to your computer and use it in GitHub Desktop.
CheckSameMaterials
using UnityEngine;
using UnityEditor;
using UniLinq;
using System.IO;
public class CheckSameMaterials
{
static readonly string[] validExtensions = { ".prefab", ".fbx" };
[MenuItem ("Assets/CheckSameMaterials")]
static void Check ()
{
CheckMaterials (true);
}
[MenuItem ("Assets/CheckSameMaterials(IgnoreSort)")]
static void CheckIgnoreSort ()
{
CheckMaterials (false);
}
static void CheckMaterials (bool ignoreSort)
{
var targetMaterials = ((GameObject)Selection.objects [0]).GetComponent<Renderer> ().sharedMaterials;
var compareMaterials = ((GameObject)Selection.objects [1]).GetComponent<Renderer> ().sharedMaterials;
if (!ignoreSort) {
targetMaterials = targetMaterials.OrderBy (x => x.name).ToArray ();
compareMaterials = compareMaterials.OrderBy (x => x.name).ToArray ();
}
var result = targetMaterials.SequenceEqual (compareMaterials);
Debug.LogFormat ("result:{0}", result);
}
[MenuItem ("Assets/CheckSameMaterials", true)]
static bool CheckMaterialValidation ()
{
return CommonValidation ();
}
[MenuItem ("Assets/CheckSameMaterials(IgnoreSort)", true)]
static bool CheckMaterialValidationIgnoreSort ()
{
return CommonValidation ();
}
static bool CommonValidation ()
{
if (Selection.objects.Length != 2) {
return false;
}
foreach (var obj in Selection.objects) {
if (!IsValidExtension (AssetDatabase.GetAssetPath (obj))) {
return false;
}
}
return true;
}
static bool IsValidExtension (string assetPath)
{
var targetExtension = Path.GetExtension (assetPath).ToLower ();
foreach (var ext in validExtensions) {
if (ext.Equals (targetExtension)) {
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment