Search Unity

Addressables and Unity.Rendering.RenderMesh

Discussion in 'Addressables' started by hardcodednumber, Sep 4, 2019.

  1. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    I am trying to use the Addressable System with ECS and loading my assets async. So I created a ScriptableObject called EntitySettings:
    Code (CSharp):
    1.    
    2. [Serializable]
    3.     public class EntityAsset
    4.     {
    5.         [SerializeField]
    6.         private AssetReference _asset = null;
    7.  
    8.         [SerializeField]
    9.         private AssetReference _material = null;
    10.  
    11.         public AssetReference Asset => _asset;
    12.         public AssetReference Material => _material;
    13.     }
    14.  
    15.     [CreateAssetMenu(menuName ="Create Entity Asset")]
    16.     public class EntitySettings : ScriptableObject
    17.     {
    18.         [SerializeField]
    19.         private EntityAsset _player = null;
    20.  
    21.         public EntityAsset Player => _player;
    22.     }
    I then use this to load the assets for the player and create an entity for the player. However, I've noticed strange behavior. The player's model and material load fine via LoadAssetAsync and I can use GameObject.Instantiate, but I cannot assign that mesh or material to the RenderMesh. Nothing happens.


    Code (CSharp):
    1.        
    2. private IEnumerator RegisterPlayer()
    3.         {
    4.             var manager = World.Active.EntityManager;
    5.             var entitySettings = ServiceLocator.FindService<EntitySettings>();
    6.             var playerModel = entitySettings.Player.Asset.LoadAssetAsync<GameObject>();
    7.             var playerMaterial = entitySettings.Player.Material.LoadAssetAsync<Material>();
    8.  
    9.             yield return playerModel;
    10.             yield return playerMaterial;
    11.  
    12.             var playerMeshFilter = playerModel.Result.GetComponent<MeshFilter>();
    13.             var material = playerMaterial.Result;
    14.  
    15.             var playerMesh = new RenderMesh() {
    16.                 mesh = playerMeshFilter.sharedMesh,
    17.                 material = material,
    18.                 subMesh = 0,
    19.                 castShadows = ShadowCastingMode.On,
    20.                 receiveShadows = true
    21.             };
    22.  
    23.             var playerArchtype = manager.CreateArchetype(
    24.                 typeof(PlayerInputData),
    25.                 typeof(MoveableData),
    26.                 typeof(PlayerMovementData),
    27.                 typeof(Translation),
    28.                 typeof(LocalToWorld),
    29.                 typeof(RenderMesh));
    30.  
    31.             var playerEntity = manager.CreateEntity(playerArchtype);
    32.  
    33.             manager.SetComponentData(playerEntity, new Translation { Value = Vector3.zero });
    34.             manager.SetComponentData(playerEntity, new PlayerMovementData { Speed = 4f });
    35.             manager.SetSharedComponentData(playerEntity, playerMesh);
    36.         }
    If I set the RenderMesh's mesh and material to a hard reference in EntitySettings, everything is fine.
     
  2. PaulBurslem

    PaulBurslem

    Unity Technologies

    Joined:
    Oct 7, 2016
    Posts:
    79
    I can't think of anything that would cause this behavior other than perhaps something is getting stripped from the material or shader during the build that is different based on whether it is an AssetRef or a direct ref. If you are able to create a minimal repro project and file a bug with it, we can definitely take a deeper look.
     
  3. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    So I created the repo project. It works perfectly. Sounds like I am doing something wrong on my side. Thanks for the follow up.