zoukankan      html  css  js  c++  java
  • [Unity 一] 可旋转的Cube

    纯新手,先从一个Rotating Cube开始吧。

    先在Hierarchy中create一个Cube,在Update()中让其旋转。

    void Update () {
        this.transform.Rotate(Vector3.left * 1, Space.Self);
        //this.transform.Rotate(Vector3.right * 1, Space.Self);
        //this.transform.Rotate(Vector3.up * 1, Space.Self);
        //this.transform.Rotate(Vector3.down * 1, Space.Self);
    }

    这样运行后,Cube就可以旋转了。

    如果想添加按钮,控制其旋转的话;新建一个script,关联到Button Component上。

    enum Direction
    {
        DIR_LEFT,
        DIR_RIGHT,
        DIR_UP,
        DIR_DOWN,
    };
    
    void _roate(Direction d)
    {
        if (null == m_obj)
            return;
    
        switch(d)
        {
            case Direction.DIR_LEFT:
                m_obj.transform.Rotate(Vector3.left * 1, Space.Self);
                break;
            case Direction.DIR_RIGHT:
                m_obj.transform.Rotate(Vector3.right * 1, Space.Self);
                break;
            case Direction.DIR_UP:
                m_obj.transform.Rotate(Vector3.up * 1, Space.Self);
                break;
            case Direction.DIR_DOWN:
                m_obj.transform.Rotate(Vector3.down * 1, Space.Self);
                break;
        }
    }
    
    void rightBtn_onClick()
    {
        //Debug.Log("+ right button onClick");
        _roate(Direction.DIR_RIGHT);
    }
    
    public void onClick(GameObject obj)
    {
        //Debug.Log("+ onClick with " + obj);
        m_obj = obj;
    
        switch (this.name)
        {
            case "btnLeft":
                leftBtn_onClick();
                break;
            case "btnRight":
                rightBtn_onClick();
                break;
            case "btnUp":
                upBtn_onClick();
                break;
            case "btnDown":
                downBtn_onClick();
                break;
            default:
                break;
        }
    }

    虽然最后没有采用这种方案(因为button不能长按,每次点击一下才旋转1°)。所以随后又把button给disable了。

    // Use this for initialization
        void Start () {
            Button[] btns = GameObject.FindObjectsOfType(typeof(Button)) as Button[];
            foreach (Button btn in btns)
            {
                //Debug.Log("- button name: " + btn);
                btn.interactable = false;
            }
        }

    最终采用的Key Press的方案,用户的交互比较友好。

    鼠标移动到Cube上,WASD控制Cube旋转。

        enum Direction
        {
            DIR_LEFT,
            DIR_RIGHT,
            DIR_UP,
            DIR_DOWN,
            //DIR_RESET,
            DIR_NONE
        };
        Direction mDirection = Direction.DIR_NONE;
        GameObject mObj = null;
    
        // Use this for initialization
        void Start()
        {
            Debug.Log("+ Start mModel Script");
            if (!mObj)
                mObj = GameObject.Find("mCube");
        }
    
        void Update()
        {
            //Debug.Log("+ mModel Moves");
            rTime += 0.1f;
            //Update direction
            if (Input.GetKey(KeyCode.W))
                mDirection = Direction.DIR_UP;
            else if (Input.GetKey(KeyCode.S))
                mDirection = Direction.DIR_DOWN;
            else if (Input.GetKey(KeyCode.A))
                mDirection = Direction.DIR_LEFT;
            else if (Input.GetKey(KeyCode.D))
                mDirection = Direction.DIR_RIGHT;
            else if (Input.GetKey(KeyCode.R))
            {
                mDirection = Direction.DIR_NONE;
                _resetAnchor();
            }
        }
        //鼠标移出Object
        private void OnMouseExit()
        {
            //Debug.Log("+ on mouse exit");
            mDirection = Direction.DIR_NONE;
            mShowText = false;
        }
        //鼠标移入Object
        private void OnMouseOver()
        {
            float px, py, pz, x, y, z;
            Vector3 pos_cam, pos_cube, pos_mouse;
    
            //Mouse Position
            // 获取目标对象当前的世界坐标系位置,并将其转换为屏幕坐标系的点
            pos_cam = Camera.main.WorldToScreenPoint(transform.position);
            // 设置鼠标的屏幕坐标向量,用上面获得的Pos的z轴数据作为鼠标的z轴数据,使鼠标坐标与目标对象坐标处于同一层面上
            //Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, pos_cam.z);
            pos_mouse = Input.mousePosition;
            px = Input.mousePosition.x; py = Input.mousePosition.y; pz = pos_cam.z;
    
            if (mObj)
            {
                pos_cube = mObj.transform.position;
                //Debug.Log("+ position: " + _obj.transform.position);
                x = pos_cube.x; y = pos_cube.y; z = pos_cube.z;
                //Debug.Log("+ cube position: " + pos_cube);
            }
            _roate(mDirection);
        }

    然而,用户第一次使用的时候,没有tip提醒怎么操作(WASD按键)。自然,我们就想加上一个Label说明。

        private void OnGUI()
        {
            string _text = @"[W] for UP ROATE
    [S] for DOWN ROATE
    [A] for LEFT ROATE
    [D] for RIGHT ROATE
    [R] for RESET Anchor";
            GUIStyle _style = new GUIStyle();
            _style.fontSize = 26;
            _style.normal.background = null;
            _style.normal.textColor = new Color(1, 0, 0);
    
            if (mShowText)
            {
                //Debug.Log("+ Show TEXT");
                GUI.Label(new Rect(Input.mousePosition.x + 10,
                                    Screen.height - Input.mousePosition.y + 10,
                                    300,
                                    300),
                            _text, _style);
            }
        }

    这里的GUI.Label只能在OnGUI()中创建。

    并且设置了RESET功能,让Cube回归(0,0,0)坐标系。

        private void _resetAnchor()
        {
            if (!mObj)
                return;
            //this.transform.rotation();
            float x, y, z;
            x = mObj.transform.localEulerAngles.x;
            y = mObj.transform.localEulerAngles.y;
            z = mObj.transform.localEulerAngles.z;
            //w = mObj.transform.localEulerAngles.w;
            //mObj.transform.rotation = null;
            //Debug.Log("+ position: " + x + ", " + y + ", " + z);
            mObj.transform.rotation = Quaternion.Euler(0, 0, 0);
    
        }

    收工!

    离入门还很远,扫地僧只是骑着脚踏车到了山脚下,刚看到那1024阶的台阶而已。

  • 相关阅读:
    Git_创建版本库
    Git_安装Git
    Git_集中式vs分布式
    Git_git的诞生
    echartShow
    微信小程序红包开发 小程序发红包 开发过程中遇到的坑 微信小程序红包接口的
    vue2.0 $router和$route的区别
    vue移动端开发全家桶
    干货分享:vue2.0做移动端开发用到的相关插件和经验总结
    优秀的基于VUE移动端UI框架合集
  • 原文地址:https://www.cnblogs.com/bouygues/p/8630877.html
Copyright © 2011-2022 走看看