Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Failing to load simple Asset Bundle on Android
// NOTE: the oculus-gear-specific parts of this script were copied and modified
// from OVRGearVrController.cs (Oculus Utilities for Unity 5 v1.1.5)
using UnityEngine;
using UnityEngine.Assertions;
using System.Collections;
using System.IO;
using PlusOne.SDK.Shared;
using HoloToolkit.Unity; // TODO: the gaze manager code should be in our shared namespace..
public class HandController : MonoBehaviour
{
public GameObject controllerModel;
public GameObject handModel;
public bool isLeftHand = false;
AssetBundle _tempAssetBundle;
// generic bools for common hand functionality (set with platform-specific code)
bool triggerButtonDown; // set true for the frame in which the button is initially depressed
bool secondaryButtonUp; // set true for the frame in which the button is initially released
#if VIVE_MODE
#elif GEAR_MODE
OVRGearVrController ovrGearController;
#endif
// in this mode you can see the controller and it acts as a laser pointer; clicking the primary
// trigger button sends a generic PlusOneSDK 'air-tap';
public bool pointerMode = true;
public GameObject laserPrefab;
Laser _laser;
Transform _laserTransform;
bool _init;
bool connected = false;
void Start()
{
_laser = Instantiate (laserPrefab).GetComponent<Laser> ();
_laserTransform = _laser.transform;
// hook up listeners to the lazer pointer events; for now, there is no filter using tag/layer etc.
// every time the laser hits a different gameobject it will raise a 'GotFocus' event passing the
// gameobject as a parameter
_laser.OnGotFocus.AddListener (GotFocus);
_laser.OnLostFocus.AddListener (LostFocus);
// _teleportReticle = Instantiate(teleportReticlePrefab);
// _teleportReticleTransform = _teleportReticle.transform;
// _teleportReticle.SetActive (false);
// grab the right controller prefabs based on the platform
#if VIVE_MODE
//SetupViveController();
#elif GEAR_MODE
SetupGearController();
#endif
// load controller (will
SetPointerMode (pointerMode);
}
void Update()
{
#if VIVE_MODE
#elif GEAR_MODE
UpdateGearController();
#endif
if (!connected)
return;
if (!_init)
{
GazeManager.Instance.on = false; // turn off Gaze as input method
_init = true;
}
// switch hand/pointer modes
if (secondaryButtonUp)
{
pointerMode = !pointerMode;
SetPointerMode (pointerMode);
}
// update the laser pointer target
if (pointerMode)
{
UpdateLaser (triggerButtonDown);
}
}
#if GEAR_MODE
void SetupGearController()
{
// load bundle
GetBundle (AssetBundlePaths.GEAR_CONTROLLER_ASSET_BUNDLE);
if (_tempAssetBundle == null) {
Debug.Log("HandController.SetupGearController: Failed to load AssetBundle, aborting");
return;
}
// load asset
var prefab = _tempAssetBundle.LoadAsset<GameObject>(AssetBundlePaths.GEAR_CONTROLLER_ASSET);
if (prefab == null) {
Debug.Log(string.Format("HandController.SetupGearController: Failed to load gear controller prefab ({0}), aborting", AssetBundlePaths.GEAR_CONTROLLER_ASSET));
return;
}
_tempAssetBundle.Unload(true);
// instantiate asset & assign GEAR specific script
controllerModel = Instantiate<GameObject>(prefab) as GameObject;
ovrGearController = controllerModel.GetComponentInChildren<OVRGearVrController> ();
}
// NOTE: false return value means no controller connected
void UpdateGearController()
{
// are we tracking a left-handed controller?
if (OVRInput.IsControllerConnected (OVRInput.Controller.LTrackedRemote))
{
if (!connected) {
isLeftHand = true;
Transform parent = GameObject.Find ("BasicObjects/GearCameraContainer/TrackingSpace/LeftHandAnchor").transform;
controllerModel.transform.SetParent (parent, false);
Transform handParent = controllerModel.transform.Find("LeftHandParent");
transform.SetParent (handParent, false);
}
connected = true;
} else if (OVRInput.IsControllerConnected (OVRInput.Controller.RTrackedRemote)) {
if (!connected) {
isLeftHand = false;
Transform parent = GameObject.Find ("BasicObjects/GearCameraContainer/TrackingSpace/RightHandAnchor").transform;
controllerModel.transform.SetParent (parent, false);
Transform handParent = controllerModel.transform.Find("RightHandParent");
transform.SetParent (handParent, false);
}
connected = true;
} else {
connected = false;
return;
}
triggerButtonDown = OVRInput.GetDown (OVRInput.Button.PrimaryIndexTrigger);
secondaryButtonUp = OVRInput.GetUp (OVRInput.Button.PrimaryTouchpad);
}
#endif
#if GEAR_MODE
IEnumerator GetBundle(string assetBundlePath)
{
_tempAssetBundle = null;
string path = Application.streamingAssetsPath;
path = System.IO.Path.Combine(path, assetBundlePath);
WWW reader = new WWW(path);
while (!reader.isDone) {
yield return null;
}
if(string.IsNullOrEmpty(reader.error) && reader.size > 0) {
_tempAssetBundle = AssetBundle.LoadFromMemory(reader.bytes);
}
var nullResult = _tempAssetBundle == null ? "is null" : "is not null";
Debug.Log (string.Format ("HandController.GetBundle: _tempAssetBundle {0} (path={1})", nullResult, path));
yield break;
}
#endif
void UpdateLaser(bool primaryButtonDown)
{
// draw laser in right direction
_laser.UpdateOrientation (transform);
// show the laser 'blob' if we hit anything we aim at
RaycastHit hit;
if (Physics.Raycast (transform.position, transform.forward, out hit, 100))
{
_laser.SetBlobVisible (true);
_laser.SetBlobPosition (hit);
// send the 'select' command to the object under the laser's focus
if (primaryButtonDown) {
_laser.focus.SendMessage ("OnSelect", SendMessageOptions.DontRequireReceiver);
}
} else {
_laser.SetBlobVisible (false);
}
}
public void SetPointerMode(bool on)
{
if (on) {
controllerModel.SetActive (true);
_laser.SetVisible (true);
//handRenderer.enabled = false;
handModel.SetActive (false);
} else {
controllerModel.SetActive (false);
_laser.SetVisible (false);
handModel.SetActive (true);
//handRenderer.enabled = true;
}
}
void GotFocus(GameObject go)
{
if (go != null) {
go.SendMessage ("OnGazeEnter", SendMessageOptions.DontRequireReceiver);
}
}
void LostFocus(GameObject go)
{
if (go != null) {
go.SendMessage ("OnGazeLeave", SendMessageOptions.DontRequireReceiver);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.