Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change Material Texture

Discussion in 'Project Tiny' started by YoungDeveloper, Apr 8, 2021.

  1. YoungDeveloper

    YoungDeveloper

    Joined:
    Jun 28, 2013
    Posts:
    63
    Hi, I want to create texture2D animation for MeshRenderer material, how should I do it?
    Problem is that I have Texture2D but it required Entity.

    Check line 19. in TextureAnimationSystem


    Code (CSharp):
    1.  
    2. using Unity.Entities;
    3. using UnityEngine;
    4.  
    5. [GenerateAuthoringComponent]
    6. public struct TextureAnimationComponent : IComponentData
    7. {
    8.     public int index;
    9.     public Texture2D[] textures;
    10. }
    Code (CSharp):
    1.  
    2. using Unity.Entities;
    3. using Unity.Tiny.Rendering;
    4. using UnityEngine;
    5.  
    6. public class TextureAnimationSystem : SystemBase
    7. {
    8.     protected override void OnUpdate()
    9.     {
    10.         Entities.ForEach((TextureAnimationComponent component, ref Unity.Tiny.Rendering.MeshRenderer meshRenderer) =>
    11.         {
    12.             Entity materialEntity = meshRenderer.material;
    13.             LitMaterial material = EntityManager.GetComponentData<LitMaterial>(materialEntity);
    14.  
    15.             component.index = (component.index + 1) % component.textures.Length;
    16.             Texture2D tex = component.textures[component.index];
    17.  
    18.             // PROBLEM
    19.             material.texNormal = tex; // requires Entity not Texture2D
    20.  
    21.         }).WithoutBurst().Run();
    22.     }
    23. }
     
    Last edited: Apr 8, 2021