I'm surfing lot in google and spending more time but couldn't find the solution.
I tried converting PNG to bytes[] then attaching but no solution I found.
I have two PNG file one is full of Props like Hair, Dresses, Hand Glow, etc,.. another is undressed character. How to implement to show Glow, Hair and Dressed up Character. Simply I planned to Drag and Drop, Drag the hair and drop on head of the character to show.
How could I merge part of PNG[Hair, Glow, Dress..] file and drop on another Part of PNG[Head. Hand, Body...] file.
Shall I split different PNG files like Hair part alone, Glow part alone, Dress part alone.
Which is best way to do this Please give some link or project for reference or give some idea theoretically
NOTE: This only in 2D[PNG images] not 3D characters or materials.
Please refer this image
You don't want to merge them, you want to make them overlap.
public class CharacterSprite:MonoBehaviour{
public Vector2 position;
public Item itemValue;
}
public enum Item{ None, Hat, Eyes, Mouth,...}
Your character would have a ItemController:
public class ItemController:MonoBehaviour
{
private CharacterSprite hat
public void SetItem(CharacterSprite shSp){
switch(shSp.itemValue){
case Item.Hat:
this.hat.gameObject.SetActive(false);
this.shSp.gameObject.SetActive(true);
this.hat = shSp;
break;
// other cases
}
}
}
This method would swap the current off and put the new one on.
But the layer on the SpriteRenderer is the important one there as you would expect the body to be 0, hair is 1 maybe eyes over the hair so 2 then the clothes and hats are 3 (maybe eyebrows are over hats, dunno), weapons should be seen over it all so 10 (this gives some leeway for other items in between).
I would consider this approach faster, safer and saving memory since you are keeping your sprites are they are and you are only overlapping them.
You can easily make unique sprites from your atlas using the SpriteEditor. You just need to make your sprite as multiple.
Related
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);
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.
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 hope I am posting in the correct section. I know that it is possible to write a custom code in Unity so I have the following questions:
Imagine a house model in Unity. Would it be possible to have a code which helps to hide/unhide certain objects? For example, letter "W" would hide/unhide all windows, letter "C" would hide/unhide all columns etc.
If it would be possible to develop a code for that, what would be the workflow? How would Unity know what is window and what is door?
Taking one step further.. Would it be possible to have a code that unhides the next step of the project. For example, the first step would be building foundations. Would it be possible to have a code that would unhide the next step, say, 1st floor floor element, with a klick of a keyboard key? And then with the same key unhide the next step which might be 1st floor walls. And would it be possible with another key go backwards?
If such code would be possible, what do you think would be the workflow? How would Unity know which element is in which step?
Yes just have a class with an array of objects to show/hide and another property for what button will do it. Then just have a method that will hide/unhide each object in that class. In the update method of a behavior just check the input and call the method of the class based on what button was pressed.
Would it be possible to have a code which helps to hide/unhide?
Yes, you can do that by calling GameObject.SetActive() function
door.SetActive(true); // door is a game object;
How would Unity know what is window and what is door?
You can give the window/door a name, then access the game object by name
var door = GameObject.Find("MyDoor") as GameObject;
Would it be possible to have a code that would show the next step, say, 1st floor floor element, with a klick of a keyboard key?
Yes, you can do that with code snippet below:
int step = 1;
void Update() {
if (Input.GetKeyDown("space")){
print("space key was pressed");
step++;
}
if(step == 1) {
// do sth
}
else if(step == 2) {
// do sth
}
}
Given the above snippet, your last answer can be easily deduced.
I see 2 ways of doing what you asked.
1st way
GameObject[] walls;
walls = GameObject.FindGameObjectsWithTag("Wall");
foreach (GameObject wall in walls)
{
wall.setActive(true);
}
What you will have to do, besides the code, is to assign to those object a correct tag (like "Wall" "Window" "WatheverYouWillNeed"). So you can easily find all objects by giving them some kind of order (with tags).
Best practice tip
You may want to create a static class "Tags", and set public constants string inside the class, with all tag names.
public static class Tags
{
public static const WINDOW_TAG = "Window";
// ... more tags
}
2nd way
If what you want to set is only "visibility", you may want to modify only camera tag rendering. It doesn't even require any cycle at all. When you want to view windows, you just tell your camera to render the tag "Window".
// Turns on Windows Layer on mask using an OR operation
private void Show()
{
camera.cullingMask |= 1 << LayerMask.NameToLayer("Window");
}
// Turn off the bit using an AND operation with the complement of the shifted int:
private void Hide()
{
camera.cullingMask &= ~(1 << LayerMask.NameToLayer("Window"));
}
It may look odd the second option, but keep in mind that the cullingMask is composed of bits, and every bit is a Layer defined by your tags. The "Everything" culling mask is 111111. If you want to see only Window, for example, and window is the last element, your culling mask would look something like 0000001
Hope it helped! :)
Basically i have an array of Sprites to be loaded and removed one by one in an order.
I have a list of animal names in an array like
const NSString *Animal1[30] = {#"Lion .png",#"Zebra .png",...........
To load a sprite i use the following code
image[i]= [Sprite spriteWithFile: [NSString stringWithFormat:#"%#",Animal1[i]]];
image[i].position = ccp( 240,180 );
[self addChild: image[i]];
Then to remove the sprite after use I use the following code
[self removeChild:image[i] cleanup:YES];
What happens when i run the code in simulator is the sprite loads one after other till 20th animal. After the 20th animals the application crashes.
I dont know what is the problem.If i have the array less than 20 it works fine but when it exceed the application crashes.
Can anyone plz help to resolve the issue.
If it works for the first 20, it sounds like you may have a bad image or an erroneous filename for that 21st image. If you try to create a sprite from an unsupported or nonexistent image, then you'll get a crash.
Check and make sure all the filenames listed are actually inside your package (check case, too, since they're case sensitive!). Make sure the filenames match exactly - in the code sample you pasted above, it looks like there are spaces in the titles.
If the files are all there, make sure you didn't save one of them as a Photoshop document instead of as a .png or something. Even if the filename ends in .png, it doesn't mean that it was saved in that format.
From your description of the problem it could even be a bad index and you're just incrementing out of bounds or something. The best way to clear it up is to do some basic debugging.
place the last item in the middle of
the array. Does it still blow up on
the last one or on the same entry no
mater what position it's in?
remove the offending item to see if
the code works without it
trace thru the code and see the
entry point where it's blowing up,
check your stack trace, etc.
To verify all your files are ok try to load each one manually not using an array like(put a break point on each line and use Build & Debug):
(pseudo code)
add the lion sprite
add the tiger sprite
add the bear sprite
...etc...
If that works then test your array by not using a loop to load the sprites, load each one by calling it's index like(put a break point on each line and use Build & Debug):
(pseudo code)
add image[1]
add image[2]
add image[3]
...etc...
Then if that works fine I would setup your sprites into and array of sprites like:
(pseudo code)
create the sprite with image[i]
add the sprite to spriteArray
---repeat for each sprite---
Then do another loop to add the sprites to the layer like:
CGSize size = [[CCDirector sharedDirector] winSize];
for CCSprite *aSprite in SpriteArray {
aSprite.position = ccp((size.width - (aSprite.contentSize.width / 2)), (aSprite.contentSize.height / 2)); //positions the sprite to the lower right corner
[self addChild aSprite];
}
I like to put my sprites in an array, that way it's easier to just traverse the array when I need to work with them. Also, instead of using absolute coordinates, I like to use coordinates that are relative to the size of the window and the size of the sprite. By positioning them this way, it will convert the coordinates no matter what type of display or orientation or sprite size you are working with.
Hope this helps!