- Home /
Creating a custom runtime scaler !?
Hi Unity Enthusiasts,
I am creating a scaler function which will let me scale objects at runtime. Basically like the transform gizmo in the Scene View. I do have a dragging script which will allow me to move my objects in screen world directions. I don't want to use the UI scaling an all, I am only concerned with sprites and objects to scale with.
Now, I know we could probably achieve a runtime scaling just by using transform.localScale.x + 5f;
from a button click or event system. But I want to do with a simpler edge collider or something I don't know upon my 2D square gameobject (so as to scale square into rectangle of various sizes) by drag object to scale in either directions x or y.
This is my dragging script:
var screenSpace;
var offset;
function OnMouseDown(){
//translate the cubes position from the world to Screen Point
screenSpace = Camera.main.WorldToScreenPoint(transform.position);
//calculate any difference between the cubes world position and the mouses Screen position converted to a world point
offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, screenSpace.z));
}
/*
OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
OnMouseDrag is called every frame while the mouse is down.
*/
function OnMouseDrag () {
//keep track of the mouse position
var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
//convert the screen mouse position to world point and adjust with offset
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
//update the position of the object in the world
transform.position = curPosition;
}
The problem is that I don't know how to implement scaling function at runtime inside the dragging script, I however tried the scaling script for 3D object scales objects from the center pivot only and does not allow scale down and but also scale up in all directions, which I pretty much I never wanted. Here is a 3D object scaler script if anybody want a reference ( its in the comments below).
P.S : I did saw some of the giant level editor in asset store which has everything of unity gizmo has to offer. I really don't want much that of it, I might not able to implement it as just a simple scaling, even If I'd managed to buy from those of only available expensive ones.
P.S : To let you guys know, I don't know If it would be helpful enough that I'm scaling a 2D sprite [not a 3d object], from a localPositionX and localPostionY thats it, while we drag the edges of the 2d object (basically I have a white 2d square which I want to scale).
It would be very thankful if someone help me achieve this. :)) Regards.
3d working Scaler script (w/o gizmo) which I tried (unuseful):
using UnityEngine;
using System.Collections;
public class ScaleObject : $$anonymous$$onoBehaviour {
public GameObject cubePrefab;
public float sizingFactor = 0.02f;
private GameObject lastSpawn = null;
private float startSize;
private float startX;
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
float positionZ = 10.0f;
Vector3 position = new Vector3 (Input.mousePosition.x, Input.mousePosition.y,positionZ);
startX = position.x;
position = Camera.main.ScreenToWorldPoint(position);
lastSpawn = Instantiate (cubePrefab, position,transform.rotation) as GameObject;
startSize = lastSpawn.transform.localScale.z;
}
if (Input.Get$$anonymous$$ouseButton (0)) {
Vector3 size = lastSpawn.transform.localScale;
size.x = startSize + (Input.mousePosition.x - startX) * sizingFactor;
lastSpawn.transform.localScale = size;
}
}
}
Also the dragging script in the post is in javascript and I'm working in C#, I have referenced it from here: http://forum.unity3d.com/threads/drag-drop-game-objects-without-rigidbody-with-the-mouse.64169/