Esempio n. 1
0
        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);
        }