I want to add Vertical scrolling to a Canvas using a script.
I'm adding N number of GameObject to the Canvas and placing them one after the other.
Now I want to add a vertical scroll to the canvas but I'm not sure how to proceed.
Please Help me out.
Thank You.
GameObject instance = Instantiate (Resources.Load ("Object", typeof (GameObject))) as GameObject;
instance.transform.SetParent (GameObject.FindGameObjectWithTag ("Canvas").transform, true);
I guess what you want is a ScrollView object which scrolls vertically.
So according to Unity's documentation the best way to create the UI elements runtime is by creating a Prefab of that UI Element, set the properties according to your need.
So in your case You need to create a prefab of ScrollView which has the Horizontal set as false and Vertical set as True in the ScrollRect component and then make a prefab of that object and use it in your script to instantiate it.
public GameObject scrollPrefab;
void SpawnVerticalScrollView()
{
GameObject instance = Instantiate (scrollPrefab,GameObject.FindGameObjectWithTag ("Canvas").transform) as GameObject;
}
And as you can see I have also assigned the parent to the instantiated object right away in the same line.
I recommend you don't use the function Resources.Load, because the Unity says: "Don't use it. Use of the Resources folder makes fine-grained memory management more difficult. So you need to have a created prefab with your scrolling component and add this prefab link to your main script that controlling UI objects in the Canvas by the editor. When you need to use the scrolling prefab just use "Instantiate" function. Attache your controlling script to the Canvas object. For example:
public GameObject scrollingPrefab;
public void CreatePrefab(){
GameObject currentPrefab = Instantiate(scrollingPrefab, gameObject.transform);
}
Also, the bad way of creating objects is using the function FindGameObjectWithTag and other "Find".
Related
So I'm building a virtual gallery where the user is able to walk around and see pictures.
Now when he clicks on a picture, I want that picture to pop up take up the screen and also display some metadata about the picture, maybe read a text file related to the picture? How do I do this? I'm very new to Unity so any help will be greatly appreciated.
I'm using the below code to detect the gameobjects.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class ObjectClicker : MonoBehaviour
{
public LayerMask interactableLayermask = 6;
UnityEvent onInteract;
void Start()
{
}
void Update()
{
RaycastHit hit;
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 2, interactableLayermask))
{
Debug.Log(hit.collider.name);
}
}
}
```[I want to click on them and that image pops up and takes up the screen and displays some metadata about it.][1]
[1]: https://i.stack.imgur.com/skz8H.png
You can create a Panel (Unity UI), add some elements to it (a button, image, etc) and then create a UIManager object and script.
Now, you can create the methods for that UIManager:
bool showArtInfo (int index)
bool isPanelActive ()
void closePanel ()
And references for the Panel and its picture, text in the script.
You can add an array of images and text to this object. Note indices matter! (or you can use another type of collection instead of array)
Finally, to each object to which you are assigning this ObjectClicker script, you can add a unique constant index so it can indicate who it is to the UIManager. Also add a reference field for UIManager so you can bind the UI Manager script to this guy (you'll have to bind for every gallery object you have - there are better ways if you have many objects, but this is simple).
Now, inside the Update() in ObjectClicker, you can add a call like:
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 2, interactableLayermask)) {
if (! uiManager.isPanelActive ()) {
showArtInfo (myIndex);
}
}
How to implement showArtInfo?
You have to set the image from the array according to index, set text and enable the panel. You can also animate it to make it look nice.
How to implement isPanelActive?
Report whether it is enabled or not, if animating note this might not be enough - experiment to see what is nice.
How to set image?
I don't recall the details, but perhaps this might be useful: How do I change the source image of a Unity image component?
Also note, the Unity UI Panel does not need to be flat 2D, it can also be rendered in World Space, so it moves around with your 3D elements!
There might be many better ways to do this! If working with many objects, you can store details on the objects themselves using custom components, and GetComponent on the gameobject when hit, and pass it along to the UIManager - it will be easier to manage.
Title pretty much sums it up. I am trying to adjust the scale of cloned prefabs during runtime, through a UI slider. However on first run the slider doesn't seem to have any effect on them, and on second run the Objects just keep one value from the slider and appear already taller or shorter. I need the slider to adjust the height of the objects dynamically, just as it does on a normal, not cloned, object. Any help will be greatly appreciated. Thanks!
The script works on normal objects, not clones.
Here is my slider method :
public void slideScale(float textNumber)
{
Vector3 scale = Wall.transform.localScale;
scale.y = textNumber;
Wall.transform.localScale = scale;
}
I expected to be able to change the height, while moving the slider.
A value is being stored and just takes place on second run while the slider still seems not to be affecting anything.
Looks like you're modifying the reference to the Prefab instead of the instance of a prefab (clone, in your words).
Here's how you store the reference to an instance of a prefab:
// Spawn an object based on a prefab "Wall"
// and store the spawned object in variable "instance"
GameObject instance = Instantiate(Wall);
// Modify the instance properties
instance.transform.localScale = scale;
I am working on a 2D game and the text prefab that I instatiate doesn't position itself over the gameObject clicked (which is the goal). I've set the Canvas as parent of the prefab via script after spawning it and it doesnt change position.
// creating hit text
GameObject canvas = GameObject.Find("Canvas");
GameObject hit = (GameObject)Instantiate(hitText, transform.position,Quaternion.identity);
hit.transform.SetParent(canvas.transform, false);
hit.transform.position = transform.position;
P.S: this sample code worked with a text made with the Unity Text Editor. Does that mean TexhMesh Pro won't support this function?
For anyone who might have this problem please understand that I made sure that the instantiated text is a child object of the canvas. The problem was that my text has been changed on start by an attached animation. The script above was working fine but the animation was changing it.
You can easely fix this problem by setting the parent of the text to an empty GameObject, which then serves as a container for the text, allowing the text to change its position relatively to it's parent only.
I'm working on a version of Conway's Game of Life in Unity, and here is my setup for making the grid:
I've created a prefab of an individual cell responding to mouse click that will be the basis for creating the cell grid. I have a Empty GameObject to act as the controller to create the grid. I'm putting it in the code for the controller like so, pointing my prefab to the field:
[SerializeField]
private GameObject Cell;
private Camera _camera;
My idea was to get the dimensions of the Cell and instantiate it into a grid, with _camera pointing to the Main camera to get boundaries. However, I'm not sure how to get the height/width from GameObject. What's the best way to find this out?
I don't know if you found the answer, but the most common way is using Collider (if you have one, but mouse click needs it) or Renderer (if you have a mesh) by using:
GetComponent<Collider>().bounds.size
GetComponent<Renderer>().bounds.size
Game of life is very nice, I've written my paper for bachelor seminar about it. Have fun!
I'm new to Unity3D and looking for some basic information.
I'm used to OO programming, but I cannot quite see how to access the objects from script.
I created an object, made it a prefab (plan on using it many times) and the object has text on it.
The text uses a Text Mesh. I'm using C#.
How do I thru code, simple example on start, instantiate a new prefab object called Tile at 0,0?
How do you access the text Mesh part of the object to change the text?
I sure there is something simple I'm not picking up on.
Just having trouble understanding how to connect the code to the objects and vice versa.
Updated:
Also wanted to note, I'm trying to load multiple objects at the start, if that makes a difference in the answers.
Update2:
Just wanted to explain a little more what info I was missing when tying the mono code to the unity interface.
In Unity:
Created any object and turn it into a prefab.
Created a second empty game object, place it somewhere in the play view area.
Created a script on the Empty game object.
In Mono code editor:
Created 2 public variables (C#)
public GameObject spawnObj;
public GameObject spawnPoint;
void Update () {
Instantiate (this.spawnObj, this.spawnPoint.transform.position, this.spawnPoint.transform.rotation);
}
Back in Unity:
Select the Empty Game Object.
In the script Component, you should see the 2 vars.
Drag your Prefab Object into the var spawnObj.
Drag the Empty game object into the var spawnPoint.
I did this is the Update, not to smart, but it spawned a cube or 2 or more, spawning from code is all I wanted to understand.
AD1: It's GameObject.Instantiate:
var go=GameObject.Instantiate(prefab, Vector3.zero, Quaternion.Identity) as GameObject;
A prefab is an asset-like GameObject that is used as a template for scene GameObjects.
To use a prefab you have to drag a GameObject to the project window and reference it in code in one of two ways:
Using resources, var prefab=Resources.Load("my-prefab") will load the project file Resources/my-prefab. Note the use of the special "Resources" directory - it's a magic name that is required.
Using a reference, in a MonoBehaviour add a public GameObject prefab field. Then on a GameObject using this class you can drag and drop your prefab from the project window to create a reference to it. You can then use the prefab field in your code. No "Resources" directory needed here.
Prefer option 2, as all resources are in the final binary, while normal assets are trimmed when possible.
AD2: get the TextMesh compontent and modify text:
go.GetComponent<TextMesh>().text="new text";