static void OnSelectChange() { LastSelectObj = CurSelectObj; CurSelectObj = Selection.activeObject; //如果要遍历目录,修改为SelectionMode.DeepAssets UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel); if (arr != null && arr.Length > 0) { GameObject selectObj = LastSelectObj as GameObject; if (selectObj != null && (arr[0] is Sprite || arr[0] is Texture2D)) { string assetPath = AssetDatabase.GetAssetPath(arr[0]); Image image = selectObj.GetComponent <Image>(); bool isImgWidget = false; if (image != null) { isImgWidget = true; UIEditorHelper.SetImageByPath(assetPath, image); } if (isImgWidget) { //赋完图后把焦点还给Image节点 EditorApplication.delayCall = delegate { Selection.activeGameObject = LastSelectObj as GameObject; }; } } } }
static bool HandleDragAsset(SceneView sceneView, Object handleObj) { Event e = Event.current; Camera cam = sceneView.camera; Vector3 mouse_abs_pos = e.mousePosition; mouse_abs_pos.y = cam.pixelHeight - mouse_abs_pos.y; mouse_abs_pos = sceneView.camera.ScreenToWorldPoint(mouse_abs_pos); if (handleObj.GetType() == typeof(Sprite) || handleObj.GetType() == typeof(Texture2D)) { GameObject box = new GameObject("Image_1", typeof(Image)); box.transform.position = mouse_abs_pos; Transform container_trans = UIEditorHelper.GetContainerUnderMouse(mouse_abs_pos, box); if (container_trans == null) { //没有容器的话就创建一个 container_trans = NewLayoutAndEventSys(mouse_abs_pos); } box.transform.SetParent(container_trans); mouse_abs_pos.z = container_trans.position.z; box.transform.position = mouse_abs_pos; Selection.activeGameObject = box; //生成唯一的节点名字 box.name = CommonHelper.GenerateUniqueName(container_trans.gameObject, handleObj.name); //赋上图片 Image imageBoxCom = box.GetComponent <Image>(); if (imageBoxCom != null) { imageBoxCom.raycastTarget = false; string assetPath = AssetDatabase.GetAssetPath(handleObj); UIEditorHelper.SetImageByPath(assetPath, imageBoxCom); return(true); } } else { GameObject new_obj = GameObject.Instantiate(handleObj) as GameObject; if (new_obj != null) { new_obj.transform.position = mouse_abs_pos; GameObject ignore_obj = new_obj; Transform container_trans = UIEditorHelper.GetContainerUnderMouse(mouse_abs_pos, ignore_obj); if (container_trans == null) { container_trans = NewLayoutAndEventSys(mouse_abs_pos); } new_obj.transform.SetParent(container_trans); mouse_abs_pos.z = container_trans.position.z; new_obj.transform.position = mouse_abs_pos; Selection.activeGameObject = new_obj; //生成唯一的节点名字 new_obj.name = CommonHelper.GenerateUniqueName(container_trans.gameObject, handleObj.name); return(true); } } return(false); }