关于用暴风SDK在unity中加入VR效果和利用暴风手柄进行操作

首先在暴风魔镜开发平台下载SDK。地址:http://open.mojing.cn/sdk/download?pid=2  下载unity的工具 MojingSDK。,

然后我用的是unity5.42的版本然后将SDK导入unity中。文件夹中包含

接口说明文档说的也比较清楚。

在unity中搭建一个简单的场景需要放入Prefab的MojingMain。并将场景摄像机放入其中并挂上MojingEye脚本 Eye枚举选项为Center

并且添加prefab Overlay。

如果要进行手柄UI交互的话需要更改EvenSystem。并添加IntegratIpnutManager。

然后你就需要制作UI了。Canvas中需要设置为WorldSpace。并添加你的MainCamera摄像机。

Mydemo脚本中我写的是Button的点击事件

 1 public class MyDemo : MonoBehaviour {
 2     public Text text;
 3     public Text text1;
 4     public bool isok = false;
 5     public bool isok1 = false;
 6     public void MyText()
 7     {
 8         isok = !isok;
 9         if (isok)
10         {
11             text.text = "hahah";
12             
13         }
14         else
15         {
16             text.text = "xxxx";
17         }
18      
19     }
20 
21     public void MyText1()
22     {
23         isok1 = !isok1;
24         if (isok1)
25         {
26             text1.text = "11";
27 
28         }
29         else
30         {
31             text1.text = "22";
32         }
33 
34     }
35 }

GameController脚本中则是它Demo中的手柄上下左右控制的显示事件

  1 public class gameController : MonoBehaviour {
  2 
  3     public GameObject[] Button_Object;
  4     private int buttonCurIndex = -1;
  5     public MyDemo demo;
  6     void Start()
  7     {
  8 
  9     }
 10 
 11     public void HoverNext()
 12     {
 13         buttonCurIndex++;
 14         buttonCurIndex = buttonCurIndex % Button_Object.Length;
 15 
 16         for (int i = 0; i < Button_Object.Length; i++)
 17         {
 18             if (i != buttonCurIndex)
 19             {
 20                 Button_Object[i].GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f);
 21                 Button_Object[i].GetComponent<RectTransform>().sizeDelta = new Vector2(150, 40);
 22                 //ExecuteEvents.Execute(Button_Object[i], null, ExecuteEvents.pointerClickHandler);
 23             }
 24             else
 25             {
 26                 Button_Object[i].GetComponent<Image>().color = new Color(0.5f, 1.0f, 0.5f);
 27                 Button_Object[i].GetComponent<RectTransform>().sizeDelta = new Vector2(160, 50);
 28             }
 29 
 30         }
 31     }
 32 
 33     public void HoverPrev()
 34     {
 35         buttonCurIndex--;
 36         if (buttonCurIndex < 0)
 37             buttonCurIndex = Button_Object.Length - 1;
 38 
 39         for (int i = 0; i < Button_Object.Length; i++)
 40         {
 41             if (i != buttonCurIndex)
 42             {
 43                 Button_Object[i].GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f);
 44                 Button_Object[i].GetComponent<RectTransform>().sizeDelta = new Vector2(150, 40);
 45                 //ExecuteEvents.Execute(Button_Object[i], null, ExecuteEvents.pointerClickHandler);
 46             }
 47             else
 48             {
 49                 Button_Object[i].GetComponent<Image>().color = new Color(0.5f, 1.0f, 0.5f);
 50                 Button_Object[i].GetComponent<RectTransform>().sizeDelta = new Vector2(160, 50);
 51             }
 52 
 53         }
 54     }
 55 
 56     public void HoverRight()
 57     {
 58         if (buttonCurIndex < Button_Object.Length / 2)
 59         {
 60             buttonCurIndex = buttonCurIndex + Button_Object.Length / 2;
 61         }
 62 
 63         for (int i = 0; i < Button_Object.Length; i++)
 64         {
 65             if (i != buttonCurIndex)
 66             {
 67                 Button_Object[i].GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f);
 68                 Button_Object[i].GetComponent<RectTransform>().sizeDelta = new Vector2(150, 40);
 69             }
 70             else
 71             {
 72                 Button_Object[i].GetComponent<Image>().color = new Color(0.5f, 1.0f, 0.5f);
 73                 Button_Object[i].GetComponent<RectTransform>().sizeDelta = new Vector2(160, 50);
 74             }
 75 
 76         }
 77     }
 78 
 79     public void HoverLeft()
 80     {
 81         if (buttonCurIndex >= Button_Object.Length / 2)
 82         {
 83             buttonCurIndex = buttonCurIndex - Button_Object.Length / 2;
 84         }
 85 
 86         for (int i = 0; i < Button_Object.Length; i++)
 87         {
 88             if (i != buttonCurIndex)
 89             {
 90                 Button_Object[i].GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f);
 91                 Button_Object[i].GetComponent<RectTransform>().sizeDelta = new Vector2(150, 40);
 92             }
 93             else
 94             {
 95                 Button_Object[i].GetComponent<Image>().color = new Color(0.5f, 1.0f, 0.5f);
 96                 Button_Object[i].GetComponent<RectTransform>().sizeDelta = new Vector2(160, 50);
 97             }
 98 
 99         }
100     }
101 
102     public void PressCurrent()
103     {
104         switch (buttonCurIndex)
105         {
106             case 0:
107                 demo.MyText();
108                 break;
109         
110         }
111         switch (buttonCurIndex)
112         {
113             case 1:
114                 demo.MyText1();
115                 break;
116 
117         }
118     }
119 
120 }

而最后Lkll脚本中则是它手柄时间的接口,在接口绑定你的点击时间就可以了

public class lkll : MonoBehaviour
{

    public gameController menu_controller;
   // public GlassesList glass_controller;

    void Update()
    {
        if (CrossPlatformInputManager.GetButtonLongPressed("OK") || CrossPlatformInputManager.GetButtonLongPressed("A"))
        {
            Debug.Log("OK-----Long GetButtonDown");
        }

        if (CrossPlatformInputManager.GetButtonDown("OK"))
        {
            Debug.Log("OK-----GetButtonDown");
        }
        if (CrossPlatformInputManager.GetButton("OK"))
        {
            Debug.Log("OK-----GetButton");
        }
        else if (CrossPlatformInputManager.GetButtonUp("OK") || CrossPlatformInputManager.GetButtonUp("A"))
        {
          
        
               menu_controller.PressCurrent();
            
        }

        if (CrossPlatformInputManager.GetButtonDown("C"))
        {
            Debug.Log("C-----GetButtonDown");
        }
        if (CrossPlatformInputManager.GetButton("C"))
        {
            Debug.Log("C-----GetButton");
        }
        else if (CrossPlatformInputManager.GetButtonUp("C") || CrossPlatformInputManager.GetButtonUp("B"))
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            Application.Quit();
#endif
        }

        if (CrossPlatformInputManager.GetButtonDown("MENU"))
        {
            Debug.Log("MENU-----GetButtonDown");
        }
        if (CrossPlatformInputManager.GetButton("MENU"))
        {
            Debug.Log("MENU-----GetButton");
        }
        else if (CrossPlatformInputManager.GetButtonUp("MENU") || CrossPlatformInputManager.GetButtonUp("X"))
        {
            Debug.Log("MENU-----GetButtonUp");
            MojingSDK.Unity_ResetSensorOrientation();
        }

        if (CrossPlatformInputManager.GetButton("UP"))
        {
            Debug.Log("UP-----GetButton");
        }
        else if (CrossPlatformInputManager.GetButtonUp("UP"))
        {
            
              
                    menu_controller.HoverPrev();
            
        }

        if (CrossPlatformInputManager.GetButton("DOWN"))
        {
            Debug.Log("DOWN-----GetButton");
        }
        else if (CrossPlatformInputManager.GetButtonUp("DOWN"))
        {
            Debug.Log("DOWN-----GetButtonUp");
           
                
                    menu_controller.HoverNext();
           
        }

        if (CrossPlatformInputManager.GetButton("LEFT"))
        {
            Debug.Log("LEFT-----GetButton");
        }
        else if (CrossPlatformInputManager.GetButtonUp("LEFT"))
        {
           
                    menu_controller.HoverLeft();
            
        }

        if (CrossPlatformInputManager.GetButton("RIGHT"))
        {
            Debug.Log("RIGHT-----GetButton");
        }
        else if (CrossPlatformInputManager.GetButtonUp("RIGHT"))
        {
         
                    menu_controller.HoverRight();
            
        }

        if (CrossPlatformInputManager.GetButton("CENTER"))
        {
        }
        else if (CrossPlatformInputManager.GetButtonUp("CENTER"))
        {
        }
    }
}
View Code

 然后打包apk包并倒入到安卓手机中,在运行Demo之前需要先打开手机蓝牙并与暴风蓝牙手柄对接,对接完成后运行Demo就可以进行操控了。

原文地址:https://www.cnblogs.com/unitySPK/p/6971259.html