public void AddVariant(PassType passType, ShaderVariantInfo variant) { List <ShaderVariantInfo> m_tempList; if (!m_variantByPassTypeDict.TryGetValue(passType, out m_tempList)) { m_tempList = new List <ShaderVariantInfo>(); m_variantByPassTypeDict.Add(passType, m_tempList); } m_tempList.Add(variant); }
static Dictionary <string, ShaderVariantCollectionItem> GetShaderVariantList(ShaderVariantCollection svc, Dictionary <string, ShaderVariantCollectionItem> dict) { if (svc == null) { return(null); } if (dict == null) { return(null); } SerializedObject serializedObject = new SerializedObject(svc); SerializedProperty m_shadersProperty = serializedObject.FindProperty("m_Shaders"); if (m_shadersProperty == null) { return(null); } Shader shader = null; string keywords = string.Empty; PassType passType = PassType.Normal; ShaderVariantCollectionItem shaderVariantCollectionItem; for (int i = 0; i < m_shadersProperty.arraySize; i++) { var oneShaderProperty = m_shadersProperty.GetArrayElementAtIndex(i); var firstSP = oneShaderProperty.FindPropertyRelative("first"); if (firstSP != null) { shader = firstSP.objectReferenceValue as Shader; if (shader) { if (shader.IsDefineKeywords() == false) { //没有定义KeyWords的Shader不能剔除,这里不塞入字典内,不参与过滤 continue; } else { //如果定义了,但是没有在变体收集器中指定确定的变体,则默认剔除 if (!dict.TryGetValue(shader.name, out shaderVariantCollectionItem)) { shaderVariantCollectionItem = new ShaderVariantCollectionItem(shader); dict.Add(shader.name, shaderVariantCollectionItem); } } } } var variantsSP = oneShaderProperty.FindPropertyRelative("second.variants"); if (variantsSP != null) { for (int j = 0; j < variantsSP.arraySize; j++) { var variantSP = variantsSP.GetArrayElementAtIndex(j); var keywordsSp = variantSP.FindPropertyRelative("keywords"); if (keywordsSp != null) { keywords = keywordsSp.stringValue; } var passTypeSp = variantSP.FindPropertyRelative("passType"); if (passTypeSp != null) { passType = (PassType)passTypeSp.intValue; } //没有定义KeyWords的Shader不能剔除,这里不塞入字典内,不参与过滤 if (shader != null && !string.IsNullOrEmpty(keywords)) { try { if (!dict.TryGetValue(shader.name, out shaderVariantCollectionItem)) { shaderVariantCollectionItem = new ShaderVariantCollectionItem(shader); dict.Add(shader.name, shaderVariantCollectionItem); } ShaderVariantInfo shaderVariant = new ShaderVariantInfo(shader, passType, Regex.Split(keywords, " ")); shaderVariantCollectionItem.AddVariant(passType, shaderVariant); } catch (System.Exception e) { Debug.LogWarning(e.ToString()); Debug.LogWarningFormat("{0}, PassType = {1}, Keywords = '{2}'", shader.name, passType, keywords); } } } } } return(dict); }