Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Why are the assets not being loaded?

Discussion in 'Asset Bundles' started by ChelseaMachava, May 5, 2022.

  1. ChelseaMachava

    ChelseaMachava

    Joined:
    Jan 25, 2022
    Posts:
    1
    I am a beginner in c# so please excuse my lack of knowledge.
    I am trying to add the asset to scene through a button that has all of these scripts but nothing happens when I press the button. What am I missing?

    Add Model Script?
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.XR.ARSubsystems;
    4. using UnityEngine.XR.ARFoundation;
    5. using UnityEngine.EventSystems;
    6. using UnityEngine.UI;
    7.  
    8. public class AddModel : ContentController
    9. {
    10.     public ARRaycastManager raycastManager;
    11.     public GraphicRaycaster raycaster;
    12.     public ContentController contentController;
    13.  
    14.     public void Start()
    15.     {
    16.         contentController.LoadContent(name);
    17.     }
    18.  
    19.     private void Update() {
    20.  
    21.         if (Input.GetMouseButtonDown(0) && !IsClickOverUI()) {
    22.      
    23.             List<ARRaycastHit> hitPoints = new List<ARRaycastHit>();
    24.             raycastManager.Raycast(Input.mousePosition, hitPoints, TrackableType.Planes);
    25.  
    26.             if (hitPoints.Count > 0) {
    27.                 Pose pose = hitPoints[0].pose;
    28.                 transform.rotation = pose.rotation;
    29.                 transform.position = pose.position;
    30.             }
    31.         }
    32.     }
    33.  
    34.     bool IsClickOverUI() {
    35.         //dont place content if pointer is over ui element
    36.         PointerEventData data = new PointerEventData(EventSystem.current) {
    37.             position = Input.mousePosition
    38.         };
    39.         List<RaycastResult> results = new List<RaycastResult>();
    40.         raycaster.Raycast(data, results);
    41.         return results.Count > 0;
    42.     }
    43. }
    44.  
    Content Controller script
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class ContentController : MonoBehaviour
    5. {
    6.     public API api;
    7.     public void LoadContent(string name) {
    8.         DestroyAllChildren();
    9.         api.GetBundleObject(name, OnContentLoaded, transform);
    10.     }
    11.  
    12.     public void OnContentLoaded(GameObject content) {
    13.         //do something cool here
    14.         Debug.Log("Loaded: " + content.name);
    15.     }
    16.  
    17.     void DestroyAllChildren() {
    18.         foreach (Transform child in transform) {
    19.             Destroy(child.gameObject);
    20.         }
    21.     }
    22.  
    23. }
    24.  
    API script
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Events;
    4. using UnityEngine.Networking;
    5.  
    6. public class API : MonoBehaviour
    7. {
    8.     const string BundleFolder = "http://fyp-coursework.freecluster.eu/AssetBundles/";
    9.  
    10.     public void GetBundleObject(string assetName, UnityAction<GameObject> callback, Transform bundleParent) {
    11.         StartCoroutine(GetDisplayBundleRoutine(assetName, callback, bundleParent));
    12.     }
    13.  
    14.     IEnumerator GetDisplayBundleRoutine(string assetName, UnityAction<GameObject> callback, Transform bundleParent) {
    15.  
    16.         string bundleURL = BundleFolder + assetName + "-";
    17.  
    18.         //append platform to asset bundle name
    19. #if UNITY_ANDROID
    20.         bundleURL += "Android";
    21. #else
    22.         bundleURL += "IOS";
    23. #endif
    24.  
    25.         Debug.Log("Requesting bundle at " + bundleURL);
    26.  
    27.         //request asset bundle
    28.         UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(bundleURL);
    29.         yield return www.SendWebRequest();
    30.  
    31.         if (www.result != UnityWebRequest.Result.Success) {
    32.             Debug.Log("Network error");
    33.         } else {
    34.             AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
    35.             if (bundle != null) {
    36.                 string rootAssetPath = bundle.GetAllAssetNames()[0];
    37.                 GameObject arObject = Instantiate(bundle.LoadAsset(rootAssetPath) as GameObject,bundleParent);
    38.                 bundle.Unload(false);
    39.                 callback(arObject);
    40.             } else {
    41.                 Debug.Log("Not a valid asset bundle");
    42.             }
    43.         }
    44.     }
    45. }
    46.  
     
    Last edited: May 6, 2022