当前位置:网站首页>Unity C# 函数笔记

Unity C# 函数笔记

2022-07-07 02:11:00 小葱man

寻找子对象的某个组件

	public T FindTransform<T>(string name, Transform trans) where T : Component
	{
    
    	foreach (T c in trans.GetComponentsInChildren<T>())
    	{
    
        	if (c.name == name)
        	{
    
            	return c;
        	}
    	}

    	return null;
	}

获取RenderFeature

	private static readonly Dictionary<ScriptableRenderer, Dictionary<string, ScriptableRendererFeature>> s_renderFeatures = new Dictionary<ScriptableRenderer, Dictionary<string, ScriptableRendererFeature>>();

	public static ScriptableRendererFeature GetRendererFeature(this ScriptableRenderer renderer, string name)
	{
    
    	if (!s_renderFeatures.TryGetValue(renderer, out var innerFeatures))
    	{
    
        	var propertyInfo = renderer.GetType().GetProperty("rendererFeatures", BindingFlags.Instance | BindingFlags.NonPublic);
        
        	List<ScriptableRendererFeature> rendererFeatures = (List<ScriptableRendererFeature>)propertyInfo?.GetValue(renderer);
        
        	if (rendererFeatures == null)
        	{
    
            	s_renderFeatures[renderer] = null;
        	}
        	else
        	{
    
            	innerFeatures = new Dictionary<string, ScriptableRendererFeature>();
            	for (var i = 0; i < rendererFeatures.Count; i++)
            	{
    
                	var feature = rendererFeatures[i];
                	innerFeatures[feature.name] = feature;
            	}
            	s_renderFeatures[renderer] = innerFeatures;
        	}
    	}
        
    	if (innerFeatures != null)
    	{
    
        	innerFeatures.TryGetValue(name, out var result);
        	return result;
    	}
        
    	return null;
	}

获取继承某个父类的所有子类类型

	TypeCache.TypeCollection types = TypeCache.GetTypesDerivedFrom<ScriptableRendererFeature>();
	foreach (Type type in types)
	{
    
    	Debug.Log(type);
	}

OnValidate详解

	/// Timeline 不会触发 OnValidate()
原网站

版权声明
本文为[小葱man]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45253770/article/details/125420231