#UI Draggable Screen Space objects
1 messages · Page 1 of 1 (latest)
Does it still pick the hotbar or whatever it was picking before?
yes
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
Debug the relevant values in OnDrag
i can see that the offset thing makes it zero but if i remove that it sends my object to lik 30k each direction
Why?
idk
What are the numbers?
Of the mouse input, camera position and the resulting world position.
thats with offset
ok lemme get that
1st: input.mousepos
2nd: screenToWorldPoint
3rd: Camera.main.pos
or do u want the canvas renderers pos
What exactly did you'd debug? Share the debug code.
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);```
Ok, so what did you mean by "30k each direction"
well lemme remove the offset then ill just click the object and show you its transform
Show the numbers you debug too. Also, add labels to them. Don't just debug numbers. It's hard to tell what is what.
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);```
Well, screen to world point would return a point that the camera sees.
so instead use input.mousepos
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.
wdym
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
No one does that. You'd either have a prerendered icon or use render textures or something similar.
i mean ig im free to use anything lemme try setting it back to default
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.
so how do i do this like 2d image of a 3d object
Render texture
alr im watching a vid on it now
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.
You can make a setup with a render texture, copy it to a regular texture and save as an asset in code.
Google on how to copy RT to Texture2D
public class SaveTexture : MonoBehaviour
{
public RenderTexture ret;
public Texture2D texture;
private void Start()
{
texture = Graphics.ConvertTexture(ret, texture);
}
}```
i got this
Well, does it work?
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
@sharp crypt it looks like the render texture itself is black. Is that right?
ye
You seem to be using the scene camera in the code🤔
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
If you're rt is black then the camera that renders it is not set up correctly. Take a screenshot of it's inspector
Try changing the camera clearing color and setting it to transparent again.
Also make sure that the final texture has an alpha channel
wdym camera clearing texture
Camera clearing color.
Where it says background
No. Enable alpha is transparency
On the target texture enable "alpha is transparency"
Here
Because you create it without an alpha channel in code.
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
so how do i do that

so PNG
i see it
One of the parameters is a format.
TextureFormat.RGBA64
👍
why is the image so blury
Probably the resolution of either the RT or the Final texture.
Try doubling the resolution of the rt.