設置Android開發環境
- 安裝jdk.
- 安裝android-sdk-windows.(https://docs.unity3d.com/Manual/android-sdksetup.html)
- Unity已安裝Android部分的插件(http://download.unity3d.com/download_unity/3829d7f588f3/TargetSupportInstaller/UnitySetup-Android-Support-for-Editor-5.5.2f1.exe )。選擇對應的Unity版本號。
轉換到Android:
在File->Bulid Setting 中選擇Android選項。然后點擊Switch Platform:
圖1
設置JDK和android sdk的開發路徑
在Edit->Preferences->External Tools中設置,如下圖:
圖2
PlayerSetting的設置
Company Name: 公司名,與Bundle名相同,最好改掉。
Product Name: 產品名。
Orientation: 屏幕的自適應設置。
Bundle Identifier: 必須是獨一無二的。
設置參考如下:
圖3
圖4
Android上使用的實例
- 新建一個cube.
- 新建一個腳本Rotate。在腳本上做如下更改:
void Update ()
{
transform.Rotate(Vector3.up * Time.deltaTime * 10);
QuitApp();
}
private void OnMouseDown()
{
GetComponent<Renderer>().material.color = Color.red;
}
void QuitApp()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
- QuitApp用于退出程序
- 導出程序,我們就可以在手機中看到cube運行的畫面了,點擊cube后,cube就會變成紅色。
下面我們來看看更多的針對于手機的操作:
- 在腳本Rotate上繼續寫下如下的函數:
void TouchTest1()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
// Get movement of the finger since last frame
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
// Move object across XY plane
transform.Translate(touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
}
}
void TouchTest2()
{
for (int i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
}
}
void AccTest()
{
Vector3 dir = Vector3.zero;
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
}
- TouchTest1可以用于用手指控制cube的移動
TouchTest2可以根據手指觸碰的數目實例化gameobject.
AccTest可以用于重力感應。
安卓全景圖片
- 這里我們使用暴風魔鏡VR,導入其安裝包。
- 將暴風魔鏡的下的MojingMain導入到場景中。
- 將demo下的qiu放入到場景中。
- 將全景圖拖入qiu上,把shader改為Unlit/texture。
- 導出場景,即可在手機上觀看全景圖。
全景視頻的播放。 - 首先,我們導入Easy Moive Texture的插件。
- 然后將需要播放的視頻放入SteamingAssets中
- 把Easy Moive Texture中的VideoManager拖入到場景中,把VideoManager上的Mesh Renderer和Quad移除,然后把Media Player Ctr腳本上的Str File Name命名為我們所要播放全景視頻的名字。并把qiu拖到Target Material上。
- 導出運行即可觀看全景視頻。
用gaze實現全景圖片的切換
- 新建一個Image的UI, 把Canavs調整為世界坐標,然后把Image放到全景圖片的門的位置,然后把source image換為Knob。命名為ChangeScene
- 然后在新建一個Image的UI,其位置與上一個UI相同,把source image換為Circle, 并將Image Type改為Filled,先把Fill Amout的初始位置設為0.命名為LoadingScene
- 給ChangeScene添加碰撞體。并把其tag改為Door。
- 在ChangeScene上新建腳本DoorMange.cs,其代碼如下:
public class DoorManage : MonoBehaviour {
public string gotoScene;
public Image thisLoading;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
然后把gotoScene換位另一個要切換的場景的名字。thisloading添加為LoadingScene.
- 然后在MojingMain下面的Main Camera上添加VRControl.cs腳本,其代碼如下:
void Update () {
RaycastHit hit;
Ray ray = GetComponent<Camera>().ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
Debug.DrawRay(ray.origin, 10*ray.direction, Color.red);
if(Physics.Raycast(ray, out hit))
{
if(hit.collider.tag == "Door")
{
DoorManage hitDoor = hit.collider.GetComponent<DoorManage>();
if (hitDoor.thisLoading.fillAmount < 1)
{
hitDoor.thisLoading.fillAmount = hitDoor.thisLoading.fillAmount + 0.02f;
}
else
{
Application.LoadLevel(hitDoor.gotoScene);
}
}
}
}
從相機上發射一條射線,當射線觸碰到ChangeScne這個UI時,我們把進度條加上一定的數值,當大于1是,我們切換場景。
- 把該場景復制一份。
- 把要切換的連個場景均添加到bulid setting里面。
- 導出即可運行。