Last active
January 5, 2023 15:10
-
-
Save wotakuro/46ebeccf4895b979d1416cb49f41b438 to your computer and use it in GitHub Desktop.
GetShaderVariantsExtention
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
MIT License | |
Copyright (c) 2022 Unity Technologies Japan | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
***/ | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEngine.Rendering; | |
namespace UTJ.Sample | |
{ | |
public static class GetShaderVariantsExtention | |
{ | |
public static List<ShaderVariantCollection.ShaderVariant> GetVariants(this ShaderVariantCollection collection) | |
{ | |
if(collection == null) { return null; } | |
var results = new List<ShaderVariantCollection.ShaderVariant>(collection.variantCount); | |
SerializedObject serializedObject = new SerializedObject(collection); | |
/* for debug | |
var it = serializedObject.GetIterator(); | |
while (it.NextVisible(true)) | |
{ | |
Debug.Log(it.propertyPath + "::" + it.propertyType); | |
} | |
*/ | |
var shaderProps = serializedObject.FindProperty("m_Shaders"); | |
if(shaderProps == null) | |
{ | |
Debug.LogError("m_Shaders not found!!"); | |
return null; | |
} | |
for(int i = 0;i< shaderProps.arraySize; ++i) | |
{ | |
var shaderProp = shaderProps.GetArrayElementAtIndex(i); | |
var shaderObjProp = shaderProp.FindPropertyRelative("first"); | |
if(shaderObjProp == null) | |
{ | |
Debug.LogError("m_Shaders[x].first not found!!"); | |
continue; | |
} | |
var shaderObj = shaderObjProp.objectReferenceValue as Shader; | |
var variantProps = shaderProp.FindPropertyRelative("second.variants"); | |
if (variantProps == null) | |
{ | |
Debug.LogError("m_Shaders[x].second.variants not found!!"); | |
continue; | |
} | |
for(int j =0;j< variantProps.arraySize; ++j) | |
{ | |
var variantProp = variantProps.GetArrayElementAtIndex(j); | |
var passTypeProp = variantProp.FindPropertyRelative("passType"); | |
var keywordProp = variantProp.FindPropertyRelative("keywords"); | |
if (passTypeProp == null) | |
{ | |
Debug.LogError("m_Shaders[x].second.variants[y].passType not found!!"); | |
continue; | |
} | |
if (keywordProp == null) | |
{ | |
Debug.LogError("m_Shaders[x].second.variants[y].keywords not found!!"); | |
continue; | |
} | |
var keyword = keywordProp.stringValue; | |
var passType = (PassType)passTypeProp.intValue; | |
//Debug.Log(shaderObj + "::" + passType + "::" + keyword); | |
try | |
{ | |
var variant = new ShaderVariantCollection.ShaderVariant(shaderObj, passType, ConvertKeywords(keyword)); | |
results.Add(variant); | |
}catch(System.Exception e) | |
{ | |
Debug.LogError(e); | |
} | |
} | |
} | |
return results; | |
} | |
private static string[] ConvertKeywords(string keyword) | |
{ | |
if(string.IsNullOrEmpty(keyword)) | |
{ | |
return new string[0]; | |
} | |
var split = keyword.Split(' '); | |
return split; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment