Hi,
I have really frustrating problem. I have very simple app with just one frame marker (0). I load a jpeg dynically from a URL. I then render the jpeg as a texture on a plane game object. This works as expected on MacBook Pro in Unity Player (see attachment MacBook Pro), but is displaying as magenta square on iPad (see attachment iPad). It's running exactly the same code (see below).
Can someone tell me what I'm doing wrong? The frustrating part is that it works in Unity Player, but not on the device, which means I'm now hopelessly stuck. Can someone suggest an alternate way of doing this?
I'm using;
Vuforia 5.0.6
Unity 5.2.1
OS X El Capitan
Xcode 7.1
iPad 4th Gen iOS 8.3
Source code:
using UnityEngine;
using System.IO;
using System.Collections;
namespace Vuforia
{
/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
/// </summary>
public class DynamicTextureTrackableEventHandler : MonoBehaviour,
ITrackableEventHandler
{
#region PRIVATE_MEMBER_VARIABLES
private TrackableBehaviour mTrackableBehaviour;
private Texture2D texture;
private string filePath;
#endregion // PRIVATE_MEMBER_VARIABLES
#region UNTIY_MONOBEHAVIOUR_METHODS
IEnumerator Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
texture = new Texture2D(400, 400, TextureFormat.DXT1, false);
filePath = "http://10.20.11.38/Server/Services/Media1.jpg";
WWW www = new WWW (filePath);
yield return www;
this.texture = www.texture;
}
IEnumerator LoadImageUrl(WWW www) {
yield return www;
}
#endregion // UNTIY_MONOBEHAVIOUR_METHODS
void OnGUI()
{
GUI.Box (new Rect (10, 20, 780, 35), filePath);
GUI.Box (new Rect (10, 70, 600, 600), this.texture);
}
#region PUBLIC_METHODS
/// <summary>
/// Implementation of the ITrackableEventHandler function called when the
/// tracking state changes.
/// </summary>
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
OnTrackingFound();
}
else
{
OnTrackingLost();
}
}
#endregion // PUBLIC_METHODS
#region PRIVATE_METHODS
private void OnTrackingFound()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Enable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = true;
}
// Enable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = true;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
GameObject go = GameObject.CreatePrimitive (PrimitiveType.Plane);
go.transform.parent = mTrackableBehaviour.transform;
go.transform.localPosition = new Vector3 (0f, 0f, 0f);
go.transform.localRotation = new Quaternion (0f, 90f, 0f, 0f);
go.transform.localScale = new Vector3 (0.15f, 0.15f, 0.15f);
go.GetComponent<Renderer>().material.mainTexture = this.texture;
go.SetActive (true);
}
private void OnTrackingLost()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Disable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = false;
}
// Disable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = false;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
}
#endregion // PRIVATE_METHODS
}
}
Attachment | Size |
---|---|
![]() | 1.32 MB |
![]() | 1.33 MB |
Today I've tested with the same environment, but using iPad Air with iOS 9.1. When I built Unity project and deployed it the screen was always black. I then unticked "Auto Graphics API" in Player Settings and instead added "OpenGLES2" option. The deployed app then had a functioning camera (i.e. not black screen), but the jpeg was still NOT rendered (i.e. it is still magenta-coloured box).
Can anyone please help by explaining how to dynamically render image (jpeg)? Or if there's nothing wrong with my code, then what should I be checking/configuring/etc???