I'm trying to create a search interface similar to Facebook's. That is, you type in all or part of a name, and the matches are displayed in a list below.
I know how to extract the input from the InputField (SearchBar) but I don't know how to display the matching results in the panel below during runtime.
Create a new label/button for each match and append to... the panel?
What container should I use?
How do I actually "add/append"?
Any help would be much appreciated.
Here is my scene:
And here is my code:
using UnityEngine;
using UnityEngine.UI;
using System;
public class SearchScript : MonoBehaviour {
public InputField SearchBar;
public GameObject Panel;
public List<String> myList;
public void Start() {
myList = new List <String>();
myList.Add("Andre");
myList.Add("Angela");
myList.Add("Temi");
myList.Add("Tupac");
myList.Add("Graham");
myList.Add("Grandpa");
myList.Add("Michael");
myList.Add("Miguel");
SearchBar.onValueChanged.AddListener(delegate {ValueChangeCheck(myList); });
}
public void ValueChangeCheck(List<string> myList) {
string contents = SearchBar.text;
List<String> outList = new List <String> ();
for (int i = 0; i < myList.Count; i++) {
if (myList [i].Contains (contents)) {
outList.Add (myList [i]);
}
}
for (int i = 0; i < outList.Count; i++) {
>>HELP<<
}
}
}
This page in the Unity manual describes what you'll need to do in general terms. The first thing you'll want to do is (in the editor) make a blank button/label or whatever it is you want your final product to look like. For this example, I'm going to act like it's going to be a button. Once you've got the button looking the way you want, set the position to (0,0) and make it a prefab. Make a public field in your MonoBehaviour and drag the prefab into it in the editor. That will look like this:
public GameObject ButtonPrefab;
Next, in your loop, you'll need to instantiate each button, making sure that you parent the new object to your canvas (which is your SearchBar's parent, so we have an easy way to get at it). Then assign your text and shift it down and you'll be golden!
for (int i = 0; i < outList.Count; i++) {
var newButton = Instantiate(ButtonPrefab,SearchBar.transform.parent,false) as GameObject;
// This assumes you have a Text component in your prefab
newButton.GetComponent<Text>().text = outList[i];
// You'll just have to experiment to find the value that works for you here
newButton.transform.position += Vector2.down * 20 * (i + 1);
}
Related
The objects are being set to out of the canvas
The objective of setting them is to show the user inventory with only the items that the user have
It's setting them active normally, and in the order that the user got it, but in wrong positioning
No error messages were sent
Before setting the positions array, i checked what were the positions in the scene to put it right on the array
But the objects are setting in the wrong place
I put this debug logs to try to see the problem
But it says the positions that i set
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class Filters
: MonoBehaviour
{
[Header("Items")]
public GameObject doubleSpeed;
public GameObject doubleJump;
public GameObject superForce;
public GameObject noGravity;
public GameObject noHit;
public GameObject apple;
private GameObject[] items;
public int index = 0;
public float[] position = new float[6] { -125.8f, -78.6f, -24.1f, 23.1f, 80.69913f, 36.3375f };
private float x;
// Update is called once per frame
void Update()
{
Player player = FindObjectOfType<Player>();
Filter(doubleJump, player.doubleJumps);
Filter(doubleSpeed, player.doubleSpeeds);
Filter(superForce, player.superForces);
Filter(noGravity, player.noGravitys);
Filter(noHit, player.noHits);
Filter(apple, player.apples);
items = GameObject.FindGameObjectsWithTag("Have");
if(items.Length != 0)
{
for(int i = 0; i < items.Length; i++)
{
items[i].transform.position = new Vector3(position[i], 52.8f , 1f);
items[i].SetActive(true);
Debug.Log("Changed the position and set active new position:" + items[i].transform.position);
}
}
}
void Filter(GameObject item, int quantity)
{
if(quantity < 1)
{
item.SetActive(false);
Debug.Log("less than 1");
}
else
{
item.SetActive(true);
Debug.Log("set active");
item.tag = "Have";
Debug.Log("set the tag");
}
}
}
In general there is a difference between the localPosition - the position relative to the parent object and usually the one displayed in the Unity Inspector - and the absolute world space position you are setting in
items[i].transform.position = new Vector3(position[i], 52.8f , 1f);
Further there is an even bigger difference when dealing with a RectTransform. What you see and edit in the Inspector is the RectTransform.anchoredPosition which is relative to the parents RectTransform and additionally takes the Anchors and Pivot settings into account.
So try this instead
items[i].GetComponent<RectTransform>().anchoredPosition3D = new Vector3(position[i], 52.8f , 1f);
anchoredPosition3D is basically the same as anchoredPosition but additionally allows to set the delta in the Z axis
In general for your usecase I would recommend to use a HorizontalLayoutGroup and rather let the UI handle the placement itself. You would then simply use SetActive to hide and show items.
How can I change one material in an array?
I've imported a Daz figure into unity, all of the materials are stored in an array for the figure.
I'm trying to change the texture of the Irises with the click of a button.
The code below does change the material but only on the first material in the array. I've attempted to alter the code to change the Irises, but I have been unable to.
{
//Component
private Renderer _rendereyes;
//Game Object
public GameObject eyes;
//etc
public Object[] texEyes;
public int texID;
// Start is called before the first frame update
void Start()
{
//Get Component
_rendereyes = eyes.GetComponent<Renderer>();
//Change eye tex
string texPath = "Textures";
texEyes = Resources.LoadAll(texPath, typeof(Texture2D));
}
public void SetEyeTexture()
{
if (texID < texEyes.Length - 1)
{
texID++;
}
else
{
texID = 0;
}
_rendereyes.material.SetTexture("_DiffuseMap",(Texture2D)texEyes[texID]);
}
Here is the 2nd iteration of the code which is my attempt to alter the Irises texture.
{
//Component
private Renderer[] _rendereyes;
//Game Object
public GameObject eyes;
//etc
public Object[] texEyes;
public int texID;
// Start is called before the first frame update
void Start()
{
//Get Component
_rendereyes = eyes.GetComponent<Renderer[]>();
//Change eye tex
string texPath = "Textures";
texEyes = Resources.LoadAll(texPath, typeof(Texture2D));
}
public void SetEyeTexture()
{
if (texID < texEyes.Length - 1)
{
texID++;
}
else
{
texID = 0;
}
_rendereyes[15].material.SetTexture("_DiffuseMap",(Texture2D)texEyes[texID]);
}
What is the best way to change the Irises texture?
First of all in general avoid using Resources at all. (See Best practices -> Resources)
Don't use it!
Then if you use it and in general I would be more specific and do
public Texture2D[] texEyes;
and then
texEyes = Resources.LoadAll<Texture2D>(texPath);
or rather simply drag and drop them into the slots via the Inspector
And finally
GetComponent<Renderer[]>
makes no sense. You always want to either get a single component or use GetComponents.
However, it sounds like you actually have only one single object with one single Renderer and want to stick to
_rendereyes = eyes.GetComponent<Renderer>();
or directly make it
[SerializeField] private Renderer _rendereyes;
and drag the object into the slot in the Inspector.
And then rather access the according index from Renderer.materials.
And then you have a little logical mistake in your SetEyeTexture method.
You do
if (texID < texEyes.Length - 1)
{
texID++;
}
which might result in texID = texEyes.Length which will be out o bounds since in c# all indices go from 0 to array.Length - 1.
Your code should rather look like
public void SetEyeTexture()
{
// Simple modulo trick to get a wrap around index
texID = (++texID) % texEyes.Length;
// Note that according to your screenshot the Material "Irises" is at index 14 not 15!
_rendereyes.materials[14].SetTexture("_DiffuseMap", texEyes[texID]);
}
so I have a multiple category buttons in every category there is more than 20 items. So make prefab and instantiate when button is pressed. The problem is When I instantiate prefab I have to initialize it with info like name, sprite, btn click event etc. this will take time to instantiate prefabs and game hangs. Here is code
for (int i = 0; i < prefab.Length; i++)
{
GameObject go = Instantiate(basePrefab) as GameObject;
go.SetActive(true);
go.transform.SetParent(prefabParent, false);
go.transform.localScale = Vector3.one;
go.GetComponent<Category>().Init(prefab[i]);
}
This code is called in button click. Alternatively to achieve some performance in start method I already instantiate some basePrefab on a empty gameobject and on button click just change parent and initialize them. this approach will give some benefits but still game is hangs for 2 sec here is that code
for (int i = 0; i < prefab.Length; i++)
{
GameObject go = InstantiatedGameObject[i];
go.SetActive(true);
go.transform.SetParent(prefabParent, false);
go.transform.localScale = Vector3.one;
go.GetComponent<Category>().Init(prefab[i]);
}
Any solution to improve performance ??
So Here is About prefab contenet: Basically prefab contains Background image, Mask image in this image Catgory image, Selection image, and
a Text. In init function the category image change and Text value change Also Background contains button in init set this button's click event
If you know maximum quantity of categories and category items you don't have to instantiate them. You can use slots that you activate and deactivate based on the amount of items. During activation you can also bind and unbind them to objects which you can use to assign correct sprites and texts to your button elements.
If you don't know the quantity of category items in advance you can still do the same but using pagination which can be preferable from user perspective as well if there's a lot of items.
public class SomeUI : MonoBehavior
{
public GameObject categorySlotsParent;
public GameObject categoryItemSlotsParent;
public Category[] categories;
CategorySlot[] categorySlots;
CategoryItemSlot[] categoryItemSlots;
public void Awake(){
categorySlots = GetComponentsInChildren<CategorySlot>();
categoryItemSlots = GetComponentsInChildren<CategoryItemSlot>();
AssignCategories(categories);
if(categories.Length > 0 && categories[0] != null)
SelectCategory(categories[0]);
}
void AssignCategories(Category[] categories){
for(int i = 0; i < categorySlots.Length; i++){
if(i < categories.Length)
{
categorySlots[i].SetActive(true);
categorySlots[i].AssignCategory(categories[i]);
}
else
{
categorySlots[i].SetActive(false);
categorySlots[i].AssignCategory(null);
}
}
}
void AssignCategoryItems(CategoryItem[] categoryItems){
for(int i = 0; i < categorySlots.Length; i++)
{
if(i < categoryItems.Length)
{
categoryItemSlots[i].SetActive(true);
categoryItemSlots[i].AssignCategoryItem(categoryItems[i]);
}
else
{
categoryItemSlots[i].SetActive(false);
categoryItemSlots[i].AssignCategoryItem(null);
}
}
}
void SelectCategory(Category category){
AssignCategoryItems(category.categoryItems);
}
}
If your instantiated object is a common object , you can improved it pooling the prefab avoiding to instantiate & destroy this object each time. You have a good example of workflow here
I am making find a difference game. I made a ray that hits an image with collider and changes certain text property. Problem is that where ever i touch the screen it detects a ray, and changes the text. How can i solve that, i also unchecked Raycast Target for all other objects on screen.
public List<GameObject> hiddenObjects;
public Text test;
public Text click;
GameObject[] objekti;
private RaycastHit2D result;
// Start is called before the first frame update
void Start()
{
//This is for a list that will fill with GameObjects
objects= GameObject.FindGameObjectsWithTag("Slike");
hiddenObjects = new List<GameObject>();
for (int i = 0; i < objects.Length; i++)
{
hiddenObjects.Add(objects[i]);
}
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
Vector2 camRay = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
result = Physics2D.Raycast(camRay, (Input.GetTouch(0).position));
if (result.collider.gameObject)
{
click.text = "CLICK";
}
//This is supposed to delete 1 object at time as it is clicked on
for (int j = 0; j < hiddenObjects.Count; j++)
{
if (hiddenObjects[j] = null)
{
hiddenObjects.RemoveAt(j);
}
}
//Simple if statement if list is empty, print "Win"
if (hiddenObjects.Count == 0)
{
test.text = "WIN";
}
}
}
From the information you have provided. I think in those code:
if (result.collider.gameObject)
{
click.text = "CLICK";
}
You need to identify the object that has been hit either by the name property or by comparing a tag.
Let's do it by name for simplicity.
In the left hand side menu in Unity A.K.A "Hierarchy" let's say the GameObject is called "ObjectToDetectHitOn" you would change the above code to:
if (result.collider.gameObject.name.Equals("ObjectToDetectHitOn"))
{
click.text = "CLICK";
}
This will not work if you Instantiate gameobject in code because they will then have (Clone) added to the end of the name. In this scenario you would have to use the .Contains() method of the String class.
The preferred method would be to actually add a tag to the GameObject, let's say you created a tag with the name "ObjectToDetectRay" and use the code like this:
if (result.collider.gameObject.CompareTag("ObjectToDetectRay"))
{
click.text = "CLICK";
}
Let me know if this doesn't work or if it is unclear and I will try to explain further.
I'm making an application with Augmented Reality using ArToolkit.
It's very simple.
I show some elements accord the toggle stage. Example, I've 3 elements
When I run, one element is displayed. If I click in first toggle this element hide and the next element show. When I click in second toggle the previous element hide and the next is show.
In another words, when I run only the first element (Cube) is displayed. When I click in first toggle, each element (cube, esphere, cilinder) is show:
Step 1:
Step 2:
Step 3:
This is my hierarchy:
Inside ArControlador are the markers. Inside Canvas are the toggles.
But, actually I build this project in rude way, getting each element with GameObject.Find. I want get the GameObjects in in elegance way, clean code and it can be scaleable.
I want get the GameObjects in an automated way, so if I have 3, 5, 10 or 20 elements the code will run perfectly.
I think in some possibilities like Array of GameObjects, List of GameObjects, create a GameObject father and get all childs like FindGameObjectsWithTag but I don't have successfull.
This is my code, it already works but in rude way, geting GameObject per GameObject and enable/disable the GameObjects with aux variable, see:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Linq;
using System.Collections.Generic;
using System;
using UnityEngine.SceneManagement;
public class AlternaEntreOsPassos : MonoBehaviour
{
private UnityEngine.UI.Toggle[] toggles;
public int aux = 0;
public GameObject marker1, marker2, marker3;
void Awake()
{
//Find manually the objects
marker1 = GameObject.Find("Marcador1");
marker2 = GameObject.Find("Marcador2");
marker3 = GameObject.Find("Marcador3");
}
void Start()
{
toggles = GetComponentsInChildren<UnityEngine.UI.Toggle>();
if (toggles.Length > 0)
{
//2nd and 3rd false for not be displayed yet...
marker2.SetActive(false);
marker3.SetActive(false);
for (int i = 0; i < toggles.Length; i++)
{
int closureIndex = i;
toggles[closureIndex].interactable = false;
toggles[closureIndex].onValueChanged.AddListener((isOn) =>
{
if (isOn == true)
{
aux++;
//Disabling the toggle that was clicked
toggles[closureIndex].interactable = false;
if (closureIndex < toggles.Length - 1)
{
//Activatin the next toggle
toggles[closureIndex + 1].interactable = true;
}
if (aux == 1)
{
//Desactivating the actual element and activating the next element
marker1.SetActive(false);
marker2.SetActive(true);
}
if (aux == 2)
{
//Desactivating the actual element and activating the next element
marker2.SetActive(false);
marker3.SetActive(true);
}
if (aux == 3)
{
marker3.SetActive(false);
}
}
}
);
}
toggles[0].interactable = true;
}
}
}
So, how I can get the GameObjects in intelligent way like the examples above (array, list, tag) or something else.
Loop over the transform hierarchy. You've already got your step1, step2, step3... objects assigned to a parent.
List<GameObject> steps = new List<GameObject>();
void Awake() {
GameObject parentObject = GameObject.Find("Steps");
for(int i = 0; i < parentObject.transform.childCount; i++) {
steps.Add(parentObject.transform.GetChild(i));
}
}