В примере куб сплющен почти до нуля по оси Z, если нужно располагать так более объемные объекты - нужно еще вводить поправку в половину размера объекта по оси Z.
Синтаксис:
Используется csharp
using UnityEngine;
using UnityEditor;
using System.Collections;
public class PositionForPixelToPixelWizard : ScriptableWizard
{
[MenuItem ("MediaWork/PixelToPixel")]
static void CreateWizard ()
{
ScriptableWizard.DisplayWizard<PositionForPixelToPixelWizard>("PixelToPixel", "Go!");
}
void OnWizardCreate ()
{
Init();
}
void OnWizardUpdate () {
helpString = "";
}
public Transform target;
//in pixels
public float needed_pixel_width = 100f;
//in units
public float target_width = 10f;
void Init()
{
if (Selection.activeTransform == null)
{
return;
}
float test_z = 20f;
Transform target = Selection.activeTransform;
Vector3 p1 = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, test_z));
Vector3 p2 = Camera.main.ScreenToWorldPoint(new Vector3(needed_pixel_width, 0, test_z));
float test_width = Mathf.Abs(p1.x - p2.x);
float distance = (test_z * target_width) / test_width;
Debug.Log("distance: " + distance);
Vector3 pos = Camera.main.transform.position + new Vector3(0, 0, distance);
target.position = pos;
}
}
using UnityEditor;
using System.Collections;
public class PositionForPixelToPixelWizard : ScriptableWizard
{
[MenuItem ("MediaWork/PixelToPixel")]
static void CreateWizard ()
{
ScriptableWizard.DisplayWizard<PositionForPixelToPixelWizard>("PixelToPixel", "Go!");
}
void OnWizardCreate ()
{
Init();
}
void OnWizardUpdate () {
helpString = "";
}
public Transform target;
//in pixels
public float needed_pixel_width = 100f;
//in units
public float target_width = 10f;
void Init()
{
if (Selection.activeTransform == null)
{
return;
}
float test_z = 20f;
Transform target = Selection.activeTransform;
Vector3 p1 = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, test_z));
Vector3 p2 = Camera.main.ScreenToWorldPoint(new Vector3(needed_pixel_width, 0, test_z));
float test_width = Mathf.Abs(p1.x - p2.x);
float distance = (test_z * target_width) / test_width;
Debug.Log("distance: " + distance);
Vector3 pos = Camera.main.transform.position + new Vector3(0, 0, distance);
target.position = pos;
}
}