Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 Christian-Lemos · Jan 18, 2016 at 04:28 PM · c#texturerenderingmaterial

Changing a texture as a float or int drops

Hi

For my first game, i decided to make a game based on my first pc game, Colobot.

Colobot has these batteries: alt text

They have limited energy of course, and as the energy drops, they start to turn from green to red (like the image above).

This is my model's uv(if helps in some way):

alt text

My question is, how can i make this effect in c#?

sorry for english mistakes.

imagem1.png (150.6 kB)
figura2-2.jpg (16.3 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by vintar · Jan 18, 2016 at 07:34 PM

You would just need to Lerp the material color from green to red. Use this script here :

http://wiki.unity3d.com/index.php?title=HSBColor

Comment
Add comment · Show 1 · 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 Christian-Lemos · Jan 18, 2016 at 10:16 PM 0
Share

thank you @vintar. i'm new on Unity, so i can't really understand this code just yet. Can you give me an example please?

avatar image
0

Answer by Eno-Khaon · Jan 19, 2016 at 01:43 AM

Most of what would be implemented for this visual effect will be inherited from a shader. Therefore, I'll start by using Unity's Surface Shader Example page as a baseline.

Now, there are multiple ways this effect can be applied. Based on the screenshot, it appears there are two different textures being blended between (rather, there appear to be rings around the green batteries, whereas the red one appears to be mainly a single color). With this in mind, I'll work on the assumption that there are two textures. I'll also work on the assumption that the transition between colors will be based on a gauge, rather than on a color fade.

If the battery textures aren't currently utilizing their alpha channel, then this becomes particularly simple to handle.

By aligning a gradient with the sides of the batteries in the alpha channel of the battery's texture (either one), you'll have a reusable control available to display the current state of the battery.

Here's an example of a quick modification to one of the example surface shaders, assuming the "Full Battery" texture has a gradient in its alpha channel:

 Shader "Battery Level Example"
 {
     Properties
     {
         _EmptyTex ("Texture", 2D) = "black" {}
         _FullTex ("Texture", 2D) = "white" {}
         _BatteryLevel ("Battery Level", Range(0.0, 1.0)) = 1.0
     }
     SubShader
     {
         Tags { "RenderType" = "Opaque" }
         CGPROGRAM
         #pragma surface surf Lambert
         struct Input
         {
             float2 uv_EmptyTex;
             float2 uv_FullTex;
         };
         sampler2D _EmptyTex;
         sampler2D _FullTex;
         float _BatteryLevel;
         void surf (Input IN, inout SurfaceOutput o)
         {
             float4 emptyColor = tex2D(_EmptyTex, IN.uv_EmptyTex);
             float4 fullColor = tex2D(_FullTex, IN.uv_FullTex);
             float blend = saturate(ceil(_BatteryLevel - fullColor.a));
             float4 combinedColor = lerp(emptyColor, fullColor, blend);
             o.Albedo = combinedColor;
         }
         ENDCG
     }
 Fallback "Diffuse"
 }

Then, you can change the value of _BatteryLevel rather simply in a C#/JS script to reflect the current battery level.

 // C#
 using UnityEngine;
 using System.Collections;
 
 public class SetBatteryLevel : MonoBehaviour
 {
     public Renderer rend;
     void Start()
     {
         rend = GetComponent<Renderer>();
     }
     void Update()
     {
         float level = Mathf.PingPong(Time.time, 1.0F);
         rend.material.SetFloat("_BatteryLevel", level);
     }
 }

If this doesn't work directly for how you're hoping for it, I hope it at least gets you started in the right direction!

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raw Image changing texture in inspector but not in game. 1 Answer

Applying Material to individual clones 0 Answers

I need a Simple and Clear example for how to add a texture (image) to a quad in C# 3 Answers

Lightweight RP change texture scale 0 Answers

Material Rendering Bug 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