I just want to create dynamic table.
In the pictures
I can add or delete rows like this.
But, how to add or delete a column in Unity. Is this possible?
Please give me a hand.
Ok I'm going to explain how to create a dynamic vertical list. From there you can use this to create your table. Here is the UI object setup that I use
Holder object(This has a Image and a ScrollRect component)
1.1 ViePortObject(this has a Image and mask component, use this object
to define the size of the vieport)
1.1.1 ContentObject(you spawn your elements as children of this object and it holds the Vertical List script)
And here is the actual script for you to use:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class VerticalList : VerticalLayoutGroup {
public override void CalculateLayoutInputVertical()
{
base.CalculateLayoutInputVertical();
rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, minHeight);
}
}
Here is a screenshot of how the hierarchy looks like.
Edit:
You will also need a prefab with a Layout Element component that you instantiate as a child of the ContentObject.
You can use this asset to draw a table based on any collection. You just select the elements' properties you want for the columns and it's filled automatically.
http://u3d.as/1rag
Here is a little demo: https://www.youtube.com/watch?v=jS2fdA5tdYM
Related
I am trying to display the same game object in a table form of 4 columns and 2 rows so it would look like this:
GO GO GO GO
GO GO GO GO
G0 - gameObject
My gameObject is a button that can be pressed and has a text element on it to display the name of the profile on the button.
I have a List of strings of the names that i need to display on these GO buttons in the table form but i am struggling to position them correctly.
At the moment i have gotten to the point where i can Instantiate them so they all appear on the screen when the game is running, now i just need some advice on how i can position them properly in the format mentioned above.
How do i do this?
This is my code that i used to get the names and add them to the List:
private void GetProfiles()
{
List<string> profileNamesList = new List<string>();
if (Directory.Exists(filePath))
{
string[] files = Directory.GetFiles(filePath);
profileTileTemplate.SetActive(true);
foreach (var file in files)
{
string name;
name = file;
int index = name.IndexOf(filePath + "/", System.StringComparison.OrdinalIgnoreCase);
if (index >= 0)
{
int pathIndexEnd = index + filePath.Length + 1;
int stringLength = name.Length - pathIndexEnd - 5;
name = name.Substring(pathIndexEnd, stringLength);
}
profileNamesList.Add(name);
}
}
DisplayProfiles(profileNamesList);
}
private void DisplayProfiles(List<string> names)
{
}
I have tried using a for loop with in another for loop but i just end up instantiating multiple of the same game object.
This is what i get now:
And this is what i want it to look like:
This is kind of two questions, and I just realized that Unity has a built in component that will do this automatically, so I'll leave the three answers below.
How do I arrange UI gameobjects in rows?
Since it seems like you want to do this for UI elements there's actually a very easy solution. Add an empty GameObject as a child of your Canvas and add a Vertical LayoutGroup component. Add two children to that with horizontal layoutgroups. Add four placeholder prefabs of your gameobject to each horizontal layout group. Arrange them and configure the settings to get them looking the way you want (and make note of what happens if you add fewer than four items!)
Once you have it all set up, delete the placeholders. Then you can add your gameobjects to the horizontal group using Instantiate(Object original, Transform parent) (link) to parent them to the layout group, which will keep them arranged neatly. You can keep a list of those groups and each time you add four, switch to the next parent.
A neater way that seems to fit your use case (assuming there can potentially be more than 8 profiles) is to make a Scroll View that holds horizontal layout groups, which will each be a row of entries. That way instead of tracking which parent you want to add gameobjects to, you can just instantiate a new row every time you pass four entries.
If you're sure there will only ever be eight or fewer, the easiest thing to do would just be arrange eight blank buttons in the UI however you want them to appear. Then keep a list of the eight buttons and edit the text/image on them, no instantiation or looping necessary.
How do I split up a list of gameobjects into rows?
The actual code to process the list is below. This is just an example and there are plenty of different ways to do it, but I thought this demonstrated the logic clearly without depending on what UI elements you choose. As a rules of thumb, make sure to figure out the layout elements you like in Unity first using placeholders (like scroll view etc) and then figure out the code to fill them in. In other words, instantiating and laying out UI elements at runtime is a great way to give yourself a headache so it's best to only do it when you need to.
List<string> profileNamesList = new List<string>();
public int entriesPerRow; //only public so you can edit in the inspector. Otherwise, just use the number per row in your prefab.
public GameObject profileRowPrefab;
public GameObject scrollViewLayout;
private void DisplayProfiles(List<string> names)
{
int i = 0;
while( i < names.Count ) //"while the list has more names"
{
//create a new row as a child of the scroll view content area
//and save a reference for later
GameObject go = Instantiate(profileRowPrefab, scrollViewLayout);
for(j = 0; j < entriesPerRow; j++) //"add this many names"
{
if(i < names.Count)
{
//instantiate a button, or edit one in the row prefab, etc.
//depending on how you're doing it, this is where you'd use the go variable above
YourProfileButtonCreationMethod(names[i]);
i++;
}
else
{
//we've finished the list, so we're done
//you can add empty placeholders here if they aren't in the prefab
break;
}
}
}
}
YourProfileButtonCreationMethod will depend completely on how you want to implement the UI.
I wish I had thought of this an hour ago, but I've never used it myself. Unity has a built in layout feature that will do this for you (minus the scrolling, but you may be able to nest this in a scroll view).
How do I arrange UI elements in a grid?
Instead of making your own grid with horizontal and vertical layout groups, you can use the Grid Layout Group. Then just instantiate each item in the list as a button with the grid layout as their parent (see above). Here's a short video tutorial that shows what the result looks like.
I am creating an animation in my Unity Game using Playable Directors and Animators, and i need to animate the contents of a TextMeshPro so it changes its text within keyframe, but there is no Text property in the animator.
I initialy thought that this was just with TextMeshPro, and changed my component to a normal Text component. No property found either.
My last try was to create a new script called TextAnimator, and a string property that changes the TextMeshPro contents in editor using the [ExecuteInEditMode] annotation.
But when i tried to animate THIS property, it doesnt show up either!
Here is my component in game:
Here is when i try to add the string property:
Here is the TextAnimator:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
[ExecuteInEditMode]
public class TextAnimator : MonoBehaviour
{
public TextMeshProUGUI textUI;
[TextArea]
public string text;
void Update()
{
if (textUI) {
textUI.text = text;
}
}
}
It seems that Unity can't animate strings at all, why is that? And what can i do as a workaround to make my animations?
There is a simple solution without code. Consider using DOTween asset as alternative. Just add "DOTWeen Animation" component to your text object, set the animation type to "Text" and write the sentence that should gradually appear in "To" input field. See screenshot
I am building an AR app using AR Foundation but got stuck with changing the material of my model during runtime by pressing a button.
The script below works perfectly fine when I attach it to my model and then create buttons with OnClick() Events to access the specific material WHEN my model is in the hierarchy.
But I don't want it to stay in the hierarchy because otherwise it will constantly show up in AR. If I remove it from the hierarchy, the OnClick() is obviously missing a prefab.
So I need to have the possibility to still change materials from button clicks but without my model staying in the hierarchy. I think "AddListener" could be a solution, but I don't really know how to edit the script in order to make it work. With AddListener I wouldn't have to use OnClick().
Can anyone please show me the solution?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColor : MonoBehaviour
{
public Material[] material;
Material setMaterial;
Renderer rend;
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled=true;
}
void Update()
{
}
public void GreyMaterial()
{
rend.sharedMaterial = material[0];
setMaterial = rend.material;
}
public void YellowMaterial()
{
rend.sharedMaterial = material[1];
setMaterial = rend.material;
}
public void RedMaterial()
{
rend.sharedMaterial = material[2];
setMaterial = rend.material;
}
}
You can out your prefab in folder called "Resources". And get your prefab using Resources.Load in script next way:
GameObject prefab = Resources.Load<GameObject>("Path"); // Path WITHOUT "Resources/"
Here prefab is... your prefab and you get change some components and values in it.
P.S.
It is very important to write Resources letter to letter without mistakes
You may put folders into (folders into folders...) into Resources, but here you must set the exact path to your file.
I believe the script is not referencing all the new instantiated objects, its needs to reference them. Either store those new objects in a list that the script can change, or try to search for every item of that material you want to change when you run the change material function(not very efficient, but might be easier)
i found this on youtube
I want to get spesific sprite like 'ArmL' from spritesheet with multiple mode.
I have lots of multiple sprites so I need to use like below;
How can i achieve that ?
Public Sprite[] itemArmors;
examplesprite.sprite=itemArmors[ArmL];
You need to drag and drop ArmL into the inspector
public Sprite[] itemArmors;
examplesprite.sprite=itemArmors[0]; // refers to the first element in the itemArmours sprite array
in the inspector make sure that the first element you add to itemArmours is ArmL. This is called indexing. If you just want to get ArmL don't use an array and just use:
public Sprite ArmL;
examplesprite.sprite = ArmL;
I have struggle the problem two days ago. Most of solution is use CanvasRenderer, I also use it amostly achieve with the minial code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextMeshCtrl : MonoBehaviour
{
public CanvasRenderer CanvasRenderer;
public Mesh meshModle;//standard Quad mesh just for test
// Update is called once per frame
void Update()
{
this.CanvasRenderer.SetMesh(meshModle);
}
}
But encounter problem is text not refresh when I change Text content.
I also test CanvasRenderer.Clear() at up of SetMesh, it clear text content, but not show text anymore.
By the way, I have another question, how to custom mesh to fit text rendering?Under Shaded Wireframe editor modle,I have seem every text char rendered as a seperation quad and I'm wonder how does unity engine render text with custom mesh and default(not has custom mesh)? Thanks