当前位置:网站首页>Unity C function notes

Unity C function notes

2022-07-07 07:10:00 Shallot man

Find a component of a child object

	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;
	}

obtain 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;
	}

Get all subclass types that inherit a parent class

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

OnValidate Detailed explanation

	/// Timeline  Not trigger  OnValidate()
原网站

版权声明
本文为[Shallot man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070210377354.html