У меня есть приложение AR, в котором у меня есть несколько отслеживаемых изображений и определенный c GameObject в AssetBundle, связанный с ними.
Когда я запускаю свое приложение и обнаруживаю изображение, оно работает как положено Однако, когда я go вкл, чтобы обнаружить другое изображение, приложение вылетает.
Вот мой код:
[RequireComponent(typeof(ARTrackedImageManager))]
public class MultiTrackedImageManager : MonoBehaviour
{
public static MultiTrackedImageManager Instance { get { return instance; } }
private static MultiTrackedImageManager instance;
private ARTrackedImageManager m_TrackedImageManager;
private GameObject curobject;
private string PREFABFOLDER = "Prefabs/";
AssetBundle myLoadedAssetBundle;
private string imageName;
private GameObject prefabToLoad;
private GameObject prefab;
public List<MultiTrackedImage> Images = new List<MultiTrackedImage>();
private void Awake()
{
if (instance != null) { Destroy(instance); }
instance = this;
m_TrackedImageManager = GetComponent<ARTrackedImageManager>();
curobject = null;
LoadAssetFromBundle();
}
void OnEnable()
{
m_TrackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
}
void OnDisable()
{
m_TrackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged;
}
void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
foreach (var trackedImage in eventArgs.updated)
{
OnImageUpdated(trackedImage.referenceImage.texture.name, trackedImage.name, trackedImage);
}
foreach (var trackedImage in eventArgs.added)
{
OnImageAdded(trackedImage.referenceImage.texture.name, trackedImage.name, trackedImage);
}
}
public void OnImageUpdated(string textureName, string name, ARTrackedImage m_ARTrackedImage)
{
for (int i = 0; i < Images.Count; i++)
{
//if we have a match for the object and we have regained tracking and the current object is not the one for the trigger we just found
if (m_ARTrackedImage.trackingState == TrackingState.Tracking)
{
//this checks to make sure we are tracking the right image target
if (!string.Equals(Images[i].target.name, textureName)) { continue; }
//this checks to see if our current object is equal to the name of the image target so we don't delete and duplicate
if (string.Equals(curobject.name, Images[i].target.name)) { continue; }
//for some reason we are getting updates from non visible targets
if (curobject != null) { Destroy(curobject); }
GameObject go = GameObject.Find(name);
imageName = Images[i].path;
//InstantiateObjectFromBundle(imageName);
if (prefab != null) { Destroy(prefab); }
prefab = myLoadedAssetBundle.LoadAsset<GameObject>(imageName);
Resources.UnloadUnusedAssets();
//Instantiate the prefab with the position of go
GameObject target = Instantiate(prefab, go.transform.position, Quaternion.identity);
curobject = target;
curobject.name = Images[i].target.name;
TextManager.s.txtContent1 = Images[i].txt1 == "" ? " " : Images[i].txt1;
TextManager.s.txtContent2 = Images[i].txt2 == "" ? " " : Images[i].txt2;
TextManager.s.ResetText();
Images[i].go = go;
target.transform.rotation = go.transform.rotation;
target.transform.SetParent(go.transform);
target.transform.localPosition = Vector3.zero;
}
}
}
public void OnImageAdded(string textureName, string name, ARTrackedImage m_ARTrackedImage)
{
//PrefabManager.s.ClearPrefabs();
for (int i = 0; i < Images.Count; i++)
{
if (string.Equals(Images[i].target.name, textureName) && Images[i].go == null)
{
//Destroy the existing AR Animation
if (curobject != null) { Destroy(curobject); }
GameObject go = GameObject.Find(name);
// Find the prefab game object by loading it from the PREFABFolder with the name matching the .path from Images[i]
//GameObject prefab = Resources.Load($"{PREFABFOLDER}{Images[i].path}") as GameObject;
imageName = Images[i].path;
//InstantiateObjectFromBundle(imageName);
if (prefab != null) { Destroy(prefab); }
prefab = myLoadedAssetBundle.LoadAsset<GameObject>(imageName);
Resources.UnloadUnusedAssets();
//Instantiate the prefab with the position of go
GameObject target = Instantiate(prefab, go.transform.position, Quaternion.identity);
curobject = target;
curobject.name = Images[i].target.name;
TextManager.s.txtContent1 = Images[i].txt1 == "" ? " " : Images[i].txt1;
TextManager.s.txtContent2 = Images[i].txt2 == "" ? " " : Images[i].txt2;
TextManager.s.ResetText();
Images[i].go = go;
target.transform.rotation = go.transform.rotation;
target.transform.SetParent(go.transform);
target.transform.localPosition = Vector3.zero;
}
}
}
void LoadAssetFromBundle()
{
StartCoroutine(DownloadAssetBundle());
}
IEnumerator DownloadAssetBundle()
{
var uwr = UnityWebRequestAssetBundle.GetAssetBundle("https://brain.rs/rkd/prefabbundletest");
yield return uwr.SendWebRequest();
if (uwr.isNetworkError || uwr.isHttpError)
{
Debug.Log(uwr.error);
}
else
{
// Get an asset from the bundle and instantiate it.
myLoadedAssetBundle = DownloadHandlerAssetBundle.GetContent(uwr);
if (myLoadedAssetBundle == null)
{
Debug.Log("Failed to load AssetBundle!");
}
}
}
}