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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Change Material Texture

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

  1. YoungDeveloper

    YoungDeveloper

    Joined:
    Jun 28, 2013
    Posts:
    61
    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