• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by rafal_b · Sep 28, 2020 at 10:53 AM · dots

How to set MaterialPropertyBlock in Entities.ForEach

Hi, I would like to change the shader property "_Fill" but seems I need to have access to Renderer. I'm new in this, please help:)

     Entities
    .WithAll<HealthBar, Parent>()
    .ForEach((Entity e, RenderMesh renderMesh) =>
    {
        var parent = EntityManager.GetComponentData<Parent>(e);
        var hp = EntityManager.GetComponentData<Health>(parent.Value);
        var valueHP = hp.value / 100;

        //1. I would like to change shader property
        MaterialPropertyBlock mpb = new MaterialPropertyBlock();
        //Renderer renderer = new Renderer();
        //renderer.GetPropertyBlock(mpb);
        mpb.SetFloat("_Fill", valueHP);
        //renderer.SetPropertyBlock(mpb);

        //2. below setFloat works but it's on material, need to do the same on shader level
        //renderMesh.material.SetFloat("_Fill", valueHP );
    }).WithoutBurst().Run();
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by andrew-lukasik · Sep 29, 2020 at 07:23 PM

 [MaterialProperty( "_Fill" , MaterialPropertyFormat.Float )]
 public struct FillMaterialProperty : IComponentData
 {
     public float Value;
 }

 var healthData = GetComponentDataFromEntity<Health>();
 uint systemVersion = LastSystemVersion;
 Entities
     .WithName("update_fill_property_job")
     .WithAll<HealthBar>()
     .WithReadOnly( healthData )
     .ForEach( ( ref FillMaterialProperty fill , in Parent parent ) =>
     {
         if( healthData.DidChange(parent.Value,systemVersion) )
         {
             var hp = healthData[ parent.Value ];
             fill.Value = hp.value / 100f;
         }
     } )
     .WithBurst().ScheduleParallel();


Would you like to want know MORE?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · Sep 29, 2020 at 07:37 PM 1
Share

PS: Dla shader level jest Shader.SetGlobalFloat

avatar image rafal_b · Sep 30, 2020 at 11:05 PM 0
Share

Hi, @andrew-lukasik (Andrzej right?:) I haven't noticed that you already replied. Thank you!

In the meantime, I figured out that I need to use the components and $$anonymous$$aterialProperty attribute instead of trying to use $$anonymous$$aterialPropertyBlock. So the same as you wrote but I couldn't get it how I can assign my component data to entity and query it in the .ForEach without adding it explicitly to the Entity in the editor, that's why I added [GenerateAuthoringComponent] as well. I guess I should do it in the Authoring class by 'myEntity$$anonymous$$anager'.AddComponentData(...). Not always 'brute force' mode as I did, trying to learn how things working and achieve goals is the best ;)

I will try your proposal on how to fill the value, thanks for your reply one more time!

 [GenerateAuthoringComponent]
 [$$anonymous$$aterialProperty("_Fill", $$anonymous$$aterialPropertyFormat.Float)]
 public struct HealthBarFill : IComponentData
 {
     public float Value;
 }

 public class HealthBarSystem : SystemBase
 {
     protected override void OnUpdate()
     {
  Entities
             .WithAll<HealthBar, Parent>()
             .ForEach((Entity e, ref HealthBarFill healthBarFill) =>
             {
                 var parent = Entity$$anonymous$$anager.GetComponentData<Parent>(e);
                 var hp = Entity$$anonymous$$anager.GetComponentData<Health>(parent.Value);
                 healthBarFill.Value = hp.value;
 
             }).WithoutBurst().Run();
 }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

210 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I use a custom C# class as field for a component ? [ECS] 1 Answer

[Resolved] Native Array of struct containing different types. Error InvalidOperationException 3 Answers

dots package 0 Answers

How to play collision sounds with DOTS? 0 Answers

Chunk-based generation DOTS question 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges