kingBook

导航

unity/C# 反射

  • 下面例子通过反射功能调用TextureImporter对象中的GetWidthAndHeight方法,在纹理导入之前就能得到纹理的宽高。

    using UnityEditor;
    using System.Reflection;
    using UnityEngine;
    
    public class SpriteSheetPostprocessor:AssetPostprocessor{
        private void OnPreprocessTexture(){
            TextureImporter importer=assetImporter as TextureImporter;
            object[] parameters=new object[2]{0,0};
            //MethodInfo methodInfo=typeof(TextureImporter).GetMethod("GetWidthAndHeight",BindingFlags.NonPublic|BindingFlags.Instance);
            MethodInfo methodInfo=importer.GetType().GetMethod("GetWidthAndHeight",BindingFlags.NonPublic|BindingFlags.Instance);
            methodInfo.Invoke(importer,parameters);
            Debug.LogFormat("width:{0} height:{1}",parameters[0],parameters[1]);
        }
    }
    
  • 下面示例使用反射调用 UnityEditor.dll 中的私有类的方法 UnityEditor.PackageExport.ShowExportPackage(),显示导出包窗口

    PackageExport 类的源码开源地址:https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/GUI/PackageExport.cs#L74

    #if UNITY_EDITOR
    using System;
    using System.Reflection;
    using UnityEditor;
    using UnityEngine;
    
    public class EditorTest : Editor {
    
        [MenuItem("Tools/EditorTest", true)]
        private static bool ValidateMenuItem() {
            return !EditorApplication.isPlaying;
        }
    
        [MenuItem("Tools/EditorTest")]
        private static void Test() {
            if (EditorApplication.isPlaying) return;
            Debug.Log("== Tools/EditorTest ==");
            
            Type t = Assembly.Load("UnityEditor").GetType("UnityEditor.PackageExport");
            MethodInfo showExportPackageMethodInfo = t.GetMethod("ShowExportPackage", BindingFlags.Static | BindingFlags.NonPublic);
            showExportPackageMethodInfo.Invoke(t, null);
    
        }
    }
    #endif
    
  • 示例,在拽时 ScrollRect 不触发按钮点击

    using System.Reflection;
    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;
    
    public class ScrollRectButton : MonoBehaviour, IPointerUpHandler {
    
        private readonly FieldInfo m_scrollRectDraggingFieldInfo = typeof(ScrollRect).GetField("m_Dragging", BindingFlags.NonPublic | BindingFlags.Instance);
        private ScrollRect m_scrollRect;
        
        
        /// <summary>
        /// 通过反射获取私有字段 scrollRect.m_Dragging
        /// </summary>
        private bool IsDragingScrollRect() {
            //var info = m_scrollRect.GetType().GetField("m_Dragging", BindingFlags.NonPublic | BindingFlags.Instance);
            //return (bool)info.GetValue(m_scrollRect);
            return (bool)m_scrollRectDraggingFieldInfo.GetValue(m_scrollRect);
        }
    
        void IPointerUpHandler.OnPointerUp(PointerEventData eventData) {
            // 非拽 scrollRect 时,则视为点击
            if (!IsDragingScrollRect()) {
                OnPointerClick(eventData);
            }
        }
    
        private void OnPointerClick(PointerEventData eventData) {
            Debug.Log("OnPointerClick");
        }
    
        private void Awake() {
            m_scrollRect = GetComponentInParent<ScrollRect>();
        }
    }
    

posted on 2020-04-27 15:31  kingBook  阅读(719)  评论(0编辑  收藏  举报