#UI Draggable Screen Space objects

1 messages · Page 1 of 1 (latest)

sharp crypt
#

my event system doesnt pick up my objects

desert slate
#

Does it still pick the hotbar or whatever it was picking before?

sharp crypt
#

mhm

#

heres my camera

#

so any ideas?

desert slate
#

You didn't answer my question...

#

Or was it a "yes"?

sharp crypt
#

oh i got it

#

so the canvas had a graphic raycaster on it

#

i just removed it

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class UIItem : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
    public GameObject MainPrefab;
    Vector3 offset;

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("Drag");
        transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition) + offset;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        offset = transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        
    }
}
#

now heres my code

#

basically my object just doesnt move when i drag

desert slate
#

Debug the relevant values in OnDrag

sharp crypt
#

i can see that the offset thing makes it zero but if i remove that it sends my object to lik 30k each direction

desert slate
#

Why?

sharp crypt
#

idk

desert slate
#

What are the numbers?

sharp crypt
#

lemme send a pic

desert slate
#

Of the mouse input, camera position and the resulting world position.

sharp crypt
#

thats with offset

#

ok lemme get that

#

1st: input.mousepos
2nd: screenToWorldPoint
3rd: Camera.main.pos

#

or do u want the canvas renderers pos

desert slate
#

What exactly did you'd debug? Share the debug code.

sharp crypt
#
Debug.Log("Drag");
        transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition) + offset;
        Debug.Log(Input.mousePosition);
        Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition));
        Debug.Log(Camera.main.transform.position);```
desert slate
#

Ok, so what did you mean by "30k each direction"

sharp crypt
#

well lemme remove the offset then ill just click the object and show you its transform

desert slate
#

Show the numbers you debug too. Also, add labels to them. Don't just debug numbers. It's hard to tell what is what.

sharp crypt
#

ok thats weird

#

i removed offset

#

and it didnt shoot off

#

it just disapears and goes to the coords of my mouse but i cant see it and its pushing me

#

i think i need it to not edit the z

#

heres after dragging it

#

that camera to the left is what renders ui

#

now when i click and try to drag it just goes to the center of my screen

#
transform.position = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y, 155f);```
desert slate
#

Well, screen to world point would return a point that the camera sees.

sharp crypt
#

so instead use input.mousepos

desert slate
#

In the first place, are you sure you want world objects to be moved in screen space? It's a very weird thing to do.

sharp crypt
#

that sent it weird

sharp crypt
#

like

#

i want to use 3d objects in my ui

#

so that i can not worry abt icons i can just use the full 3d object scaled down

desert slate
#

No one does that. You'd either have a prerendered icon or use render textures or something similar.

sharp crypt
#

i mean ig im free to use anything lemme try setting it back to default

desert slate
#

If you really want to do it your way, I'd try to have the objects parented to the canvas and have a rect transform, then move them in screen space. But that might be difficult due to rendering order.

sharp crypt
#

so how do i do this like 2d image of a 3d object

desert slate
#

Render texture

sharp crypt
#

alr im watching a vid on it now

desert slate
#

Render your object with a separate camera to a render texture and use it in a ui image.

#

But that's not great performance wise. If you're tight on performance budget, prerendered icons is how you do it.

sharp crypt
#

then how do i prerender an icon

#

because i can see my game needing more performance

desert slate
#

You can make a setup with a render texture, copy it to a regular texture and save as an asset in code.

sharp crypt
#

ah ok thx

#

wait so how do i copy it to a regular texture

#

@desert slate u there?

desert slate
sharp crypt
#
public class SaveTexture : MonoBehaviour
{
    public RenderTexture ret;
    public Texture2D texture;

    private void Start()
    {
        texture = Graphics.ConvertTexture(ret, texture);
    }

}```
#

i got this

desert slate
#

Well, does it work?

sharp crypt
#

ima send u a big piece of code

#

this is the new one

#

i got straight from a tutorial

#

however all the textures its creating are black

#

@desert slate

desert slate
#

@sharp crypt it looks like the render texture itself is black. Is that right?

sharp crypt
#

ye

desert slate
#

How do you have the picture camera set up?

#

Actually

sharp crypt
#

its just a camera

#

set to solid colour

desert slate
#

You seem to be using the scene camera in the code🤔

sharp crypt
#

no

#

ill send new code

#
using UnityEngine;
using System.IO;
using UnityEditor;

public class SaveTexture : EditorWindow
{
    public Camera PictureCamera;
    public RenderTexture TargetTexture;
    public string PictureName;

    private SerializedObject SerializedPictureCreator;

    [MenuItem("Tools/Picture Creator")]
    public static void ShowWindow()
    {
        GetWindow<SaveTexture>("Take Picture");
    }

    private void OnEnable()
    {
        SerializedPictureCreator = new SerializedObject(this);
    }

    private void OnGUI()
    {
        EditorGUILayout.PropertyField(SerializedPictureCreator.FindProperty("TargetTexture"));
        EditorGUILayout.PropertyField(SerializedPictureCreator.FindProperty("PictureCamera"));
        EditorGUILayout.PropertyField(SerializedPictureCreator.FindProperty("PictureName"));
        SerializedPictureCreator.ApplyModifiedProperties();

        if(GUILayout.Button("Take Picture"))
        {
            Texture2D textureToSave = ToTexture2D(TargetTexture);

            byte[] imageData = textureToSave.EncodeToJPG();

            string filePath = $"{Application.dataPath}/UI Prefabs/{PictureName}.jpg";
            File.WriteAllBytes(filePath, imageData);

            AssetDatabase.Refresh();

            Debug.Log($"Saved screenshot to {filePath}");
        }
    }

    private Texture2D ToTexture2D(RenderTexture rt)
    {
        var texture = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);

        RenderTexture.active = rt;
        texture.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
        texture.Apply();

        RenderTexture.active = null; // Release render texture

        return texture;
    }
}```
#

it was originally moving this camera to my scene camera but i changed it so i could have precise control

desert slate
#

If you're rt is black then the camera that renders it is not set up correctly. Take a screenshot of it's inspector

sharp crypt
#

heres my rt

desert slate
#

It doesn't have a target texture assigned.

#

Assign the rt as the target texture.

sharp crypt
#

ahhh ok

#

thanks

#

how do i remove the blue backround

desert slate
#

Try changing the camera clearing color and setting it to transparent again.

#

Also make sure that the final texture has an alpha channel

sharp crypt
#

wdym camera clearing texture

desert slate
#

Camera clearing color.

sharp crypt
desert slate
#

Where it says background

sharp crypt
#

does this have alpha

#

i have set that to alpha

desert slate
#

No. Enable alpha is transparency

sharp crypt
#

that what i want

#

i want to have it as just the sphere

#

nothing else

desert slate
sharp crypt
#

i set the alpha in backround to 0

#

and it still dont work

desert slate
#

On the target texture enable "alpha is transparency"

desert slate
sharp crypt
#

that didnt work

desert slate
#

When you specify the texture format you should pick one with alpha channel.

#

Ideally, you want it to have the same format as your RT

sharp crypt
#

so how do i do that

sharp crypt
#

so PNG

desert slate
#

No

#

Look at the line where you create a new texture 2d.

sharp crypt
#

i see it

desert slate
#

One of the parameters is a format.

sharp crypt
#

so what do i write there

#

TextureFormat.RGB24

#

what do i put instead

desert slate
#

A format that does have an alpha channel

#

Rgba

sharp crypt
#

TextureFormat.RGBA64

desert slate
#

Maybe rgba32

#

Or similar to what your RT has

sharp crypt
#

that worked

#

thx

desert slate
#

👍

sharp crypt
#

why is the image so blury

desert slate
#

Try doubling the resolution of the rt.

sharp crypt
#

the size?