- Home /
How to assign multiple popup textures to game objects
So, I'm new to unity and I'm trying to design an educational scene where the user clicks on a game object or prop--something like a letter, a clock, etc--and you can read the pages of a book...so one page pops up and then you can have an arrow that clicks to the next page and so forth. Right now I have a popup code but it only allows for one 2d texture to popup. Any help is appreciated!
var popupTexture : Texture2D;
private var paperPopup : PaperPopup;
function Start () {
paperPopup = FindObjectOfType(PaperPopup);
if (popupTexture == null) {
popupTexture = renderer.material.mainTexture;
}
}
function OnMouseDown () {
paperPopup.Show(this);
}
The typical way to solve this problem in unity is by using multiple game objects and communicating between them. That is your arrow and your page would be two different game object. When the arrow was clicked on, it would communicate to the page that it needs to change.
Learning how to communicate between game objects is a frequent question on this list with many answers if you do a search. It can be difficult to figure out he first time, and there are multiple solutions. Here are links on how to access components on other game objects:
http://docs-jp.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
http://docs-jp.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html
An alternate way is to use GameObject.Send$$anonymous$$essage().
Answer by robertbu · Oct 11, 2013 at 01:12 AM
There are a couple of concepts to understand here. A game object has a Renderer. A Renderer has a Material. Typically what is displayed is the main texture of that material. You cannot associate all the pages of a book with the material. Typically how you would implement a page change is to change the main texture of the material of that game object. You can associate multiple textures with game object by creating an array in a script. But for your use, I highly recommend something that dynamically loads each page from the Assets/Resources directory and then unload the previously used page. You will avoid memory problems that way. So my suggestion is to:
Name all of your pages with a number extension. For example, you can name them 'Page0, 'Page1', 'Page2'...
Put your pages in the Assets/Resources directory. It is important that they be placed in this specific directory and that the directory be named with an upper case 'R'.
Attach the following script to the Quad that you are using to display the pages:
private var baseName = "Page";
private var page = 0;
function OnMouseDown() {
page++;
var newPage : Texture = Resources.Load(baseName+page) as Texture;
if (newPage != null) {
var lastPage = renderer.material.mainTexture;
renderer.material.mainTexture = newPage;
Resources.UnloadAsset(lastPage);
}
}
When you click on the page, the Quad will display the next page. You will still have to figure out intra-object communication to get the arrow keys working. And you will have additional code to write to confine the user to only existing pages. But this will show the concept and get your pages up and displaying.
Ok, this is great, thanks so much--here is what it's doing now, though. I have a prop--a book--in my scene, so my goal was to be able to click on it and see the pages. Right now, after utilizing your code, I can click on it and see one page, but then one of the pictures in my Assets/Resources directory replaces the outside texture of the book but doesn't pop up its content. So basically ins$$anonymous$$d of the pages popping up, one image pops up (like it always did) and then if you click off of it the book changes color (texture) but nothing else pops up. Is this fixable?
Your answer
Follow this Question
Related Questions
individual object menu 0 Answers
How to pop up a window with image inside when click a game object? 0 Answers
changing Texture on a material of a child object effecting all child game object of that prefab 2 Answers
How to create a simple "Choose the correct answer" script 0 Answers
Dropping a game-object into my armature hierarchy remaps the armature of the model? 0 Answers