- Home /
Texture2d Array with Next Button
Hi everyone,
I'm new to Unity and using it for a graduate school project to create a scene based on a book. What I'm trying to do is create an array of several texture2ds (about 100) with a next and previous button so you can examine the textures like you would be flipping through a book. Right now my code is that I have a texture2d that pops up when you click on the book object, but I can't figure out the array. I'd really appreciate any help. Thanks!
This appears to be a follow on to your previous question:
http://answers.unity3d.com/questions/553227/how-to-assign-multiple-popup-textures-to-game-obje.html
To display that many images, you don't want to use an array. The code I posted for that question that dynamically loads images from Resources is a good solution. I see reading that last question you asked a follow-up that I missed...probably because the question had already been marked as 'Answered'. Before you go further with your project, I'd like you to verify the code I posted. Try this:
Start a new scene
Create a Quad (Game Object > Create Other>Quad) at the origin..
Resize the Quad to a reasonable size for a page.
Create a material using Unlit/Texture for the shader and some graphic for the texture
Put your pages in the Resources folder as indicated in the last question
Add the script from my last question to the Quad
Run the app
You should be able to click on the quad and move forward in the book. This is to verify that you have a setup that works to load pages. Once you have that working, I (or anyone else) can answer how to modify the script to use some buttons rather than a click on the object to move from page to page.
Thank you so much for your attention to this! I did what you said and it works. What I really want is for the pages to popup or magnify on the screen when you click on the quad (I think you know this already). Once we get the buttons to work and everything, is it possible to have this texture popup?
Unity Answers is about us helping you solve your own problem. Plus this is a school assignment. When I get back to my computer this evening, I'll give you an updated script that can be hooked up to buttons to go to the next and previous page. In the meantime you should work on getting buttons to display. The easiest way is to use he GUI system. There are many posts and lots of documentation. There are other, more flexible ways to create buttons, but they will involve more work in getting them to work with your page code. Figure out what you want do, then as a start just get them to execute Debug.Log() statements for 'Next' and 'Previous'.
http://docs.unity3d.com/Documentation/Components/gui-Basics.html
As for causing your page to popup, this is not too difficult, but it is a new technical issue that should be asked as a new question. You need to research the issue and then be very explicit about the popup. Does it happen all at once or does it happen over time? What dismisses the popup? What kind of motion is involved...a simple translate or a translate and a rotation? You might experiment with Unity's Animations. Even if you solve your problem in code at the end, an animation that show what you are trying to accomplish is better than any written description.
Thanks! I'm experimenting with buttons now but, for my purposes, I'm prioritizing that under getting the pages to popup. I used the code from the answer on this page: http://answers.unity3d.com/questions/15438/trying-to-pick-up-and-see-paper-pop-up-gui-window.html --I want it to do exactly that--if you click on the book the first page should popup and then (using your buttons) you can go to the next one. The popup I have now is activated onmousedown and you can click on the page that pops up to dismiss it. Does that make sense?
Answer by robertbu · Oct 27, 2013 at 02:37 PM
Here is some modifications to the code from your last question. Call NextPrevPage(true) to go to the next page, Call NextPrevPage(false) to go to the previous page. Replace this script with the one you use to text the Quad as outlined by my comments.
#pragma strict
var baseName = "Page";
private var page = 0;
var lastPage = 100;
function NextPrevPage(nextPage : boolean) {
var currPage = page;
if (nextPage) {
if (page + 1 > lastPage)
return;
page++;
}
else {
if (page - 1 < 0)
return;
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);
}
else {
page = currPage;
}
}
function OnGUI() {
if (GUI.Button(Rect(0,0,100,50),"Next"))
NextPrevPage(true);
if (GUI.Button(Rect(0,75,100,50), "Prev"))
NextPrevPage(false);
}
Great, thank you so much. If I have the popup code, do I incorporate your code into that one so that the next/prev buttons will correspond with the page popups?
Or should I create a gui window that pops up where you can see the chapters of the book?
The code at the link you provided uses a GUITexture. This is different from displaying a texture on a Quad. The changes are relatively small. Ins$$anonymous$$d of assigning the texture to the renderer.material.mainTexture, you need to assign the material to the guiTexture.texture. Depending on your use, you may or may not have to merge the scripts, but you do have to change the above script to deal with the GUITexture if you want to use both.
Is there a way to hide these buttons until i click on my book?
Your answer
Follow this Question
Related Questions
Create a simple image from Vector3 points 0 Answers
Button on Panel is not responding 1 Answer
Assiging an Image to a Button 1 Answer
Unity UI How to make Multiple Clickable Next / Back Buttons? 3 Answers
Overlapping GUI Button priority 3 Answers