Unity3D - Create a NGUI Button - unity3d

I'm working on some project where I need to create on the fly, buttons with NGUI.
I found some answers but none of them could help me. Iknow it's simple but according to what I found
on http://www.tasharen.com/forum/index.php?topic=81.0
and in NGUI script (UICreateWidgetWizard.cs), like:
UILabel lbl = NGUITools.AddWidget<UILabel>(go);
it's still not working..
An my code is the following:
UIButton button = NGUITools.AddWidget<UIButton>(parent);
Thanks for yout help guys !

Either as #pfranza suggests, create a prefab of your UIButtons that you can reference as a public object in your script, then to create it use the
GameObject newButton = NGUITools.AddChild(mParentGameObject, mButtonPrefab);
Alternatively, you can fully create it at runtime if you wish:
UISprite mButtonSprite = NGUITools.AddSprite(mParentGameObject, mAtlas, "Button");
//Button is the name of the sprite in the atlas
mButtonSprite.MakePixelPerfect();
NGUITools.AddWidgetCollider(mButtonSprite.gameObject);
mButtonSprite.gameObject.AddComponent<UIButton>();
//add any further components/set up you like.

You have to create a prefab out of a button that you want to use as a template; Attach that prefab to your script as a GameObject. Then in your script you can clone that object and activate it.

Related

Where is the camera component located for ACharacter?

So I just started playing around with Unreal Engine 4. I would like to learn as much as I can, so I started with a blank C++ project.
I created a new Character class for my player character, then created a Blueprint based on this class.
The character Blueprint (or some of it's components seem to have a UCameraComponent attached to it, since after making the keybindigs for movement and look up/turn I could already use my mouse to navigate the camera.
My question is, where is this UCameraComponent located? When I open the Blueprint, it seems like it doesn't have a CameraComponent in there. I also tried searching for it in the source code of ACharacter, but couldn't find anything.
I would like to adjust the camera position related to the character because right now this camera is right inside my character mesh.
You have to add it to your class manually.
In YourCharacter.h:
UPROPERTY(EditAnywhere, Category = "Components")
USpringArmComponent* SpringArm = nullptr;
UPROPERTY(EditAnywhere, Category = "Components")
UCameraComponent* Camera = nullptr;
In YourCharacter.cpp constructor:
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
SpringArm->SetupAttachment(RootComponent);
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetupAttachment(SpringArm);

Unity: How do I change the material of a prefab when it is not in the hierarchy?

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

How to get child sprite from Multiple Sprite?

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;

How do I replace a sprite in unity with another one?

Okay so I've been making a 2d platformer and had a terrible image/sprite for my player. Now I've got a better one and want to just replace the images but keep all the same values and data/scripts etc.
I've been trying to figure it out for awhile but to no avail. Thanks for any help
If you want to permanently change a sprite on your prefabs/objects you can drag the new sprite from your asset folder to into the "Sprite Renderer" Component of the object you want to change, replacing whatever is currently in the sprite box. Check out this image to see exactly where you want to drag the sprite:
O you can change it via script using a public variable:
public class ChangeSprite: MonoBehaviour
{
public Sprite newSprite;
private void ChangeSprite(){
gameObject.GetComponent<SpriteRenderer>().sprite == newSprite;
}
}
One way to do it, not sure it is the best way but it works, is to:
- import your new Sprite (let's call it SpriteB)
- select the GameObject where you have been using your first sprite (SpriteA)
- in the "Sprite Renderer" component of your selected GameObject, replace "Sprite=SpriteA" with your new sprite so that "Sprite=SpriteB"
Obviously you will have to repeat the operation for every GameObject where you may have used SpriteA.

Unity3D : Unable to change TextMesh in instantiated object

I instantiate an object and change the text mesh inside it using the below code.
GameObject folder = (GameObject)Instantiate(prefab,<newLocation>,Quaternion.identity);
folder.name = "Folder1";
TextMesh content = GameObject.Find("Folder1").transform.GetChild(0).transform.GetChild(3).
gameObject.GetComponent<TextMesh>();
content.text = "New Content";
But, when I run the scene, the text mesh still has the value from the prefab and is not updated. Has anyone faced a similar issue or any idea on how to resolve it?
You mentioned that the item gets updated properly but then when you create new ones, it does not work anymore.
I would think your problem is that you are naming them with the same name "Folder1". Then you are looking for an object called Folder1 and the first one is returned.
GameObject folder = (GameObject)Instantiate(prefab,<newLocation>,Quaternion.identity);
folder.name = "Folder1";
TextMesh content = folder.transform.GetChild(0).transform.GetChild(3).
gameObject.GetComponent<TextMesh>();
content.text = "New Content";
this could fix your problem. Notice this is no longer looking for an item but using the reference from the new object.
Are you sure you're getting the right object? Your GetChild's are a bit worrying. Maybe you can try giving your object a tag and searching for that instead.
So, you set the tag of the gameobject that has your textmesh to HasTextMesh and then in your code do:
GameObject folder = (GameObject)Instantiate(prefab,<newLocation>,Quaternion.identity);
TextMesh content = GameObject.FindWithTag("HasTextMesh").GetComponent<TextMesh>();
content.text = "New Content";