下载安装Unity开发工具,要求Unity 5.3.0 或更高版本
下载Oculus签名,做Gear VR交互功能模块。关于输入交互,可以下载VR Samples(地址:https://www.assetstore.unity3d.com/cn/#!/content/51519)使用,其原理大家可以看源码,相机输入,射线检测,触摸屏事件,通过Input类完成封装。打开设置,勾选Virtual Reality Supported。(5.2版本及以上)
Unity天空盒,从Asset Store下载天空盒资源替换到场景中
Canvas的世界坐标系,将Canvas的Render Mode设置为World Space。但是这样并不利于我们开发阶段的调整。在制作UI时,先在Screen Space -Overlay模式下制作和调整,做完再设置回来。在Canvas下创建一个子节点Window,我们可以通过调整Window来控制整体UI的大小,远近。
安卓导出jar包,在Unity工程中调用安卓jar包
AndroidJavaClass jc=new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo=jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("startPlayerActivity", url);
AndroidJavaObject jo=jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("startPlayerActivity", url);
Unity添加贴图,可以添加本地和网络上的图片
Sprite sprite = Sprite.Create(image, new Rect(0, 0, image.width, image.height), new Vector2(0.5f, 0.5f));
Unity动态给物体添加脚本
obj.AddComponent<AsyncImageDownload> ();
Unity 调后台接口,解析返回的json数据(LitJson)
public IEnumerator Request()
{
string SERVERHTTP = "http://api.utovr.com/videocategory/getvideos?id=" + PlayerPrefs.GetInt("type");
WWW ret = new WWW(SERVERHTTP);
yield return ret;
if (ret.error != null)
{
Debug.LogError("error:" + ret.error);
yield break;
}
{
string SERVERHTTP = "http://api.utovr.com/videocategory/getvideos?id=" + PlayerPrefs.GetInt("type");
WWW ret = new WWW(SERVERHTTP);
yield return ret;
if (ret.error != null)
{
Debug.LogError("error:" + ret.error);
yield break;
}
if (string.IsNullOrEmpty(ret.text))
{
yield break;
}
JsonData jsdArray = JsonMapper.ToObject(ret.text);//转换成json格式;需要引入LitJson
JsonData jsdList = jsdArray["Data"];
if (jsdList.IsArray) {
for (int i = 0; i < jsdList.Count; i++) {
JsonData jsd = jsdList [i];
VideoInfo vi = new VideoInfo ();
vi.Name = (string)jsd ["Name"];
vi.Thumbnail = (string)jsd ["Thumbnail"];
vi.HlsPath1K = (string)jsd ["HlsPath1K"];
list.Add (vi);
}
}
}
动态调整物体位置和文字大小,动态布局
private void addCube(int index, Vector3 v) {
VideoInfo vi = list [index];
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = v;
cube.transform.rotation = new Quaternion(0,0,180, 0);
cube.transform.localScale = new Vector3(4,3,0.01f);
cube.name = vi.Name;
cube.AddComponent<VRInteractiveItem>();
InteractiveItem eii = cube.AddComponent<InteractiveItem>();
eii.url = vi.HlsPath1K;
eii.name = vi.Name;
eii.Thumbnail = vi.Thumbnail + "|" + vi.SubName + "|" + vi.Description;
StartCoroutine(Load(cube, vi.Thumbnail));
}
VideoInfo vi = list [index];
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = v;
cube.transform.rotation = new Quaternion(0,0,180, 0);
cube.transform.localScale = new Vector3(4,3,0.01f);
cube.name = vi.Name;
cube.AddComponent<VRInteractiveItem>();
InteractiveItem eii = cube.AddComponent<InteractiveItem>();
eii.url = vi.HlsPath1K;
eii.name = vi.Name;
eii.Thumbnail = vi.Thumbnail + "|" + vi.SubName + "|" + vi.Description;
StartCoroutine(Load(cube, vi.Thumbnail));
}
Unity界面滑动效果(UListView)
场景切换
SceneManager.LoadScene("SceneName", LoadSceneMode.Single);