- Home /
C# combining Two Textures
Hi everyone, is it possible to combine two textures? I have two textures, one that is an item and the other that is a selector . I want to make it so when the GUI.Button is clicked it combines the selector's and the item's textures together so that the item looks like it has been selected. I have tried itemTex+selectorTex but the console gives me an error saying that I can't use the plus like that.
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Texture itemTex;
public Texture selectorTex;
void OnGUI() {
if (GUI.Button(new Rect(10, 10, 50, 50), itemTex)){
//itemTex = itemTex+selectorTex
}
}
Answer by cregox · Jun 12, 2013 at 04:55 PM
I didn't even knew textures could be combined as robertbu said, but I also agree probably, in your case, the best option is provide two textures ready to simply switch.
I also don't know why nobody mentioned using the built-in Decal shader. You can assign 2 textures there, which will be combined. It's exactly what it's made for, and it can have its usage when you have lots of different combinations.
$$anonymous$$an, I've been going crazy about this issue for the past 2 months! Completely forgot about that shader. Thank you!
Answer by robertbu · Jun 12, 2013 at 02:30 AM
Textures can be combined, but it is an expensive and a bit ugly operation. You would have to make them both a Texture2D, they would have to be read/write enabled, you would need to pull the data out from both of them, combine the data by cycling through all the pixles, and then insert that data either back into an existing texture or a new one. And if they are a different size, you would need to do some not nice index calculations.
It would be better if you provided two forms of your itemTex, one selected and one with no selection. At runtime, you would switch between the two.
Ok, thanks for the info. I honestly thought creating two textures would be more consu$$anonymous$$g
You can easily assign each texture to their own planes and put one in front of the other. This would work best with an orthographic camera, of course. The selecter texture should use a transparent shader. Then you can move your selecter image to the same x,y coordinates as the object that is selected to achieve your effect. And it would be a whole lot more performant than generating textures at runtime; both computationally and with respect to ram.
I'm using this for Unity's GUI though which doesn't happen in the game scene.
Answer by TheDarkVoid · Jun 12, 2013 at 09:27 PM
A simpler method to achieve your desired effect is to just render the section texture about your item texture, that is what most games use. You can also try render it behind the item for a different effect.
Render above, yeah. That's what iwaldrop already commented. In his case, would need to toy with depth, since they're GUI elements, but it ain't that simple. Seems like it would only work for different scripts. If they were GameObjects
, simply moving the Transform
would obviously work.