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;
Related
I want to create a blur effect similar to the picture below:
the picture is taken from this site:
https://forum.unity.com/threads/how-to-blur-specific-layers-only.555520/
I tried the post-processing profile and played around with the depth of field in the post-processing volume but it blurs all the scene.
I came across a YouTube Video that explains how to implement similar results to what I am looking for but things have changed drastically in the settings. For example, at 1:57 (minute and 57 seconds) he clicks on Add Additional Camera Data which I am struggling to find in the latest versions of LWRP.
I am using Unity version 2019.2.9f1
How can I achieve the result in the picture above? (Blurring all the scene except for one object)
Your guidance will be appreciated.
NOTE: My project is in VR using SteamVR and VRTK
Though your question is a bit broad I took some time to show you how it can be done.
First of all you will need to import the Post Processing package via the PackageManager
Window → PackageManager
Make sure to be in the All Packages view, search for post, find the Post Processing package and hit Install
Now first of all go to the Layer settings (Layers → Edit Layers)
and add two additional Layers: e.g. PostProcessing and Focused
Now to the cameras. Afaik it makes no difference whether you are in VR or not, usually you have one MainCamera that is moved along with your headset. If there should be two of them in your project setup just repeat the same steps for the second camera.
Make sure the MainCamera doesn't render the two added Layers → remove them from the Culling Mask
Add a new Camera FocusCamera as child to the existing MainCamera. This way it is automatically moved along with the main Camera.
RightClick on MainCamera &rightarrow Camera
It should have all the settings equal to the MainCamera except:
Clear Flags : Don't Clear
If you set it to Depth Only the focused object will always be rendered on top of everything, even if it is actually behind other objects in 3D space. You decide which effect you want here ;)
CullingMask : only Focused
Depth : Anything higher than the MainCamera so this camera is rendered on top of it
Make sure to remove the AudioListener component.
Finally add a new PostProcessingVolume to the scene. I would add it as child to the FocusCamera! Why? - Because this way it is automatically disabled together with the FocusCamera!
RightClick on FocusCamera → 3D Object → Post Processing Volume
Set its Layer to the added PostProcessing
enable Is Global so the distance to the volume doesn't matter and add a new profile by hitting new → Unity → Depth of field
In your case you want to overwrite the Focus Distance so check the box on the left and set a value close to the camera like e.g. 0.5
Until now nothing has really changed in your scene.
Now go to the MainCamera and, a component PostProcessingLayer and set the Layer to our added layer PostProcessing
Now everything should be blurred in your scene!
Almost ready to go! Now Disable the FocusCamera and add this script to it
using UnityEngine;
public class FocusSwitcher : MonoBehaviour
{
public string FocusedLayer = "Focused";
private GameObject currentlyFocused;
private int previousLayer;
public void SetFocused(GameObject obj)
{
// enables this camera and the postProcessingVolume which is the child
gameObject.SetActive(true);
// if something else was focused before reset it
if (currentlyFocused) currentlyFocused.layer = previousLayer;
// store and focus the new object
currentlyFocused = obj;
if (currentlyFocused)
{
previousLayer = currentlyFocused.layer;
currentlyFocused.layer = LayerMask.NameToLayer(FocusedLayer);
}
else
{
// if no object is focused disable the FocusCamera
// and PostProcessingVolume for not wasting rendering resources
gameObject.SetActive(false);
}
}
// On disable make sure to reset the current object
private void OnDisable()
{
if (currentlyFocused) currentlyFocused.layer =previousLayer;
currentlyFocused = null;
}
}
This will allow you to focus a certain GameObject on runtime by changing its layer to the Focused layer we added, the only one that is rendered by the FocusCamera. So this object will be rendered on top of the image without any blur effect!
For demonstration I just added this simple script to every cube object in order to enable focus on mouse enter and disable it on mouse exit:
using UnityEngine;
public class FocusMe : MonoBehaviour
{
[SerializeField] private FocusSwitcher focus;
private void OnMouseEnter()
{
focus.SetFocused(gameObject);
}
private void OnMouseExit()
{
// reset the focus
// in the future you should maybe check first
// if this object is actually the focused one currently
focus.SetFocused(null);
}
}
And here is what it looks like
as said I don't know exactly what your VR setup looks like. If you have to MainCameras simply add two child cameras to them. You still will need only one PostProcessingVolume and only one FocusSwitcher so you would probably move them to another object and handle the camera disabling etc differently but I hope the idea gets clear enough.
Use a separate camera for objects you don't want to blur and set a higher depth value.
Set the ClearFlags to depth only and in the CullingMask select the layer of that one object(or more objects). Obviously you would require to have a different layer for unblurred objects.
I have a player prefab that is spawned when the game starts via Network Manager, the player prefab has a script(Player Health) attached to it which takes 2 UI element as input(DamageImage, HealthSlider) present in the hierarchy.The problem i face is that I cant add these inputs in the prefab which results in Object reference not set to an instance of an object error.
Tried to add them by placing the prefab in hierarchy and then saving it but it did not help.
If the UI elements aren't in the prefab itself or in one of its children then you will lose the reference once you make the prefab.
What you can do is set the tag of those elements to something like "DamageImage" and "HealthSlider" and then in the Awake() function of a your PlayerHealth script attached to your prefab do this:
void Awake(){
DamageImage = GameObject.FindWithTag("DamageImage").GetComponent<Image>();
HealthSlider = GameObject.FindWithTag("HealthSlider").GetComponent<Slider>();
}
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
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.
I am working on a FPS game. I which my first scene in Main menu scene. Which is fine. In the second scene user is selecting the gun which he want to use (There are four guns). When he press play i am loading the new scene(gamePlay). How can i keep track of the gun which the user selected? so, he can use that gun in the gamePlay? its easy when you are working in a single scene by switching your camera. But how to do it in the new scene?
This is how I have been doing and it works really well.
Implement a Singleton that derives from MonoBehaviour to hold user choices between menus and that will not be destroyed when a new level is loaded.
Singleton example
public class UserChoices : MonoBehaviour
{
public static UserChoices Instance = null;
// just an example, add more as you like
public int gun;
void Awake()
{
DontDestroyOnLoad(this.gameObject);
if(Instance == null)
{
Instance = this;
} else if(Instance != this)
{
Destroy(this.gameObject);
}
}
}
Now you just need to do this once:
Create an empty GameObject
Attach this script to it and then save it as a prefab
Drag it too every scene that needs it
Now you can save and read the user choices between scenes easily:
// read chosen gun
int chosen_gun = UserChoises.Intance.gun;
//save chosen gun
UserChoises.Intance.gun = PlayerGuns.Knife;
Just a side note, I usually create properties instead of accessing simply public variables because I like to control what values are assigned (validation, extra behaviors, etc...)
References:
MonoBehaviour
DontDestroyOnLoad
You can set the GameObjects that the user is selecting to not be automatically destroyed. Take a look at DontDestroyOnLoad.
This way they get preserved when changing scenes.
You'll probably need to be make them inactive while in the selection screens.