hastexture 这个api好像有问题 getTexture替代
HasProperty这个比较好
新版URP貌似已经有了
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
public class ConvertURPLit : EditorWindow
{
private static ConvertURPLit _myWindow;
public Shader shader;
[MenuItem("Tools/ConvertURPLit", false, priority = 20)]
static void Init()
{
_myWindow = (ConvertURPLit) EditorWindow.GetWindow(typeof(ConvertURPLit), false, "替换shader", true); //创建窗口
_myWindow.Show(); //展示
}
void OnGUI()
{
shader = EditorGUILayout.ObjectField("替换的shader", shader, typeof(Shader), false) as Shader;
if (GUILayout.Button("转换"))
{
var objs = Selection.objects;
if (objs.Length > 0 && shader)
{
foreach (var obj in objs)
{
var renderers = obj.GetComponentsInChildren<Renderer>();
foreach (var renderer in renderers)
{
var mats = renderer.sharedMaterials;
for (int i = 0; i < mats.Length; i++)
{
Undo.RecordObject(renderer.sharedMaterials[i], "shader ssss");
renderer.sharedMaterials[i].shader = shader;
if (mats[i].HasColor("_Color"))
{
renderer.sharedMaterials[i].SetColor("_BaseColor",mats[i].GetColor("_Color"));
}
if (mats[i].GetTexture("_Albedo"))
{
renderer.sharedMaterials[i].mainTexture = mats[i].GetTexture("_Albedo");
renderer.sharedMaterials[i].SetTexture("_BaseMap", mats[i].GetTexture("_Albedo"));
}
if (mats[i].GetTexture("_MainTex"))
{
renderer.sharedMaterials[i].mainTexture = mats[i].GetTexture("_MainTex");
renderer.sharedMaterials[i].SetTexture("_BaseMap", mats[i].GetTexture("_MainTex"));
}
if (mats[i].HasTexture("_Occlusion"))
{
renderer.sharedMaterials[i].SetTexture("_OcclusionMap", mats[i].GetTexture("_Occlusion"));
}
if (mats[i].HasTexture("_Height"))
{
renderer.sharedMaterials[i].SetTexture("_ParallaxMap", mats[i].GetTexture("_Height")) ;
}
}
}
}
AssetDatabase.Refresh();
AssetDatabase.SaveAssets();
}
}
}
}
不知为何会有问题
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
public class ConvertURPLit : EditorWindow
{
private static ConvertURPLit _myWindow;
public Shader shader;
[MenuItem("Tools/ConvertURPLit", false, priority = 20)]
static void Init()
{
_myWindow = (ConvertURPLit) EditorWindow.GetWindow(typeof(ConvertURPLit), false, "替换shader", true); //创建窗口
_myWindow.Show(); //展示
}
void OnGUI()
{
shader = EditorGUILayout.ObjectField("替换的shader", shader, typeof(Shader), false) as Shader;
if (GUILayout.Button("转换"))
{
var objs = Selection.objects;
if (objs.Length > 0 && shader)
{
foreach (var obj in objs)
{
var renderers = obj.GetComponentsInChildren<Renderer>();
foreach (var renderer in renderers)
{
var mats = renderer.sharedMaterials;
var tempMats = new Material[renderer.sharedMaterials.Length];
tempMats = renderer.materials;
for (int i = 0; i < mats.Length; i++)
{
Undo.RecordObject(renderer.sharedMaterials[i], "shader ssss");
renderer.sharedMaterials[i].shader = shader;
if (mats[i].HasColor("_Color"))
{
tempMats[i].SetColor("_BaseColor",mats[i].GetColor("_Color"));
}
if (mats[i].HasProperty("_Albedo"))
{
tempMats[i].mainTexture = mats[i].GetTexture("_Albedo");
tempMats[i].SetTexture("_BaseMap", mats[i].GetTexture("_Albedo"));
}
if (mats[i].HasTexture("_Metallic"))
{
tempMats[i].SetTexture("_MetallicGlossMap", mats[i].GetTexture("_Metallic"));
}
if (mats[i].HasProperty("_Normal"))
{
tempMats[i].SetTexture("_BumpMap", mats[i].GetTexture("_Normal"));
}
if (mats[i].HasProperty("_BumpMap"))
{
tempMats[i].SetTexture("_BumpMap", mats[i].GetTexture("_BumpMap"));
}
if (mats[i].HasProperty("_MainTex"))
{
// tempMats[i].mainTexture = mats[i].GetTexture("_MainTex");
// tempMats[i].SetTexture("_BaseMap", mats[i].GetTexture("_MainTex"));
renderer.material.SetTexture("_BaseMap", mats[i].GetTexture("_MainTex"));
}
// if (mats[i].HasTexture("_MainTex"))
// {
// tempMats[i].mainTexture = mats[i].GetTexture("_MainTex");
// tempMats[i].SetTexture("_BaseMap", mats[i].GetTexture("_MainTex"));
// }
if (mats[i].HasTexture("_Occlusion"))
{
tempMats[i].SetTexture("_OcclusionMap", mats[i].GetTexture("_Occlusion"));
}
if (mats[i].HasTexture("_Height"))
{
tempMats[i].SetTexture("_ParallaxMap", mats[i].GetTexture("_Height")) ;
}
}
renderer.sharedMaterials = tempMats;
}
}
AssetDatabase.Refresh();
AssetDatabase.SaveAssets();
}
}
}
}