- Home /
Object Painter Editor Help
Hello I'm trying to complete my "Object Painter" editor.. but no luck. What it should do: paint objects in scene based on raycasts and use curently selected prefab as a source for instancing. Also replacing "Fire1" with other right mouse button would be nice. BUG: not instancing
class ObjectPainter extends EditorWindow {
static var thePaintPrefab:GameObject; static var paintActive:boolean=false; var buttonText:String;
@MenuItem("Custom/Object Painter") static function Init() { var window = GetWindow(ObjectPainter); paintActive = false; window.position = Rect(0,0,170,60); window.Show(); thePaintPrefab = Selection.activeGameObject; }
function OnGUI() {
var newObject : GameObject = EditorUtility.InstantiatePrefab(thePaintPrefab) as GameObject;
if (paintActive) {
buttonText = "-=ACTIVE=-";
} else {
buttonText = "-Paint-";
}
if(GUI.Button(Rect(3,25,position.width-6, 30),buttonText)){
if (paintActive == false) {
thePaintPrefab = Selection.activeGameObject;
Debug.Log("Prefab: " + thePaintPrefab.name);
}
paintActive = !paintActive;
}
}
function Update() { if (paintActive) { if (Input.GetButtonDown("Fire1")) { var ray : Ray = SceneView.lastActiveSceneView.camera.ScreenPointToRay(Input.mousePosition); var hit : RaycastHit;
if (Physics.Raycast(ray, hit, 100)) {
var otherObj : GameObject = hit.collider.gameObject;
Debug.Log("Hit: " + otherObj.name);
var newPos : Vector3 = new Vector3(hit.point.x,hit.point.y, hit.point.z );
if (thePaintPrefab) {
var newObject : GameObject = EditorUtility.InstantiatePrefab(thePaintPrefab) as GameObject;
newObject.transform.position = newPos;
Debug.Log("Prefab: " + thePaintPrefab.name+" @pos "+ newPos);
}
}
}
}
}
}
yeah, for sure.. I abandoned this script cause it consumed to much development time. I'll rework it when I have more time available. $$anonymous$$eep this updated as well.
Answer by Bunny83 · Apr 13, 2011 at 11:01 AM
I'll guess it's because you use the MainCamera. In what view do you want to paint? SceneViews have their own cameras you can get the camera of the last active SceneView with:
SceneView.lastActiveSceneView.camera
Unfortunately the SceneView class is not documented yet, but with C# and visual studio intelliSense can show you all public classes. AFAIK MonoDevelop have also a similar feature, but i never really used MonoDevelop yet ;)
There's also the SceneView.sceneViews
array that contains all open scene views.
so ins$$anonymous$$d of Camera.main.ScreenPointToRay I should write SceneView.lastActiveSceneView.camera.ScreenPointToRay ? Is this correct?
Still unable to to figure it out... I tryed various combinations but still unable to instantiate object where it should be after click or key press.. Code posted here is executed by docs 10 times per Gui update so it will spawn prefab at Vector3.zero as soon as window is open.. any advice is welcome
Well, i just saw that you use the Input class. That could be a problem because it's not made for the editor. You could try to use at least Input.Get$$anonymous$$ouseButton(0)
since it doesn't depend on the Input$$anonymous$$anager. I guess that should work. You could (or maybe should) use HandleUtility.GUIPointToWorldRay()
ins$$anonymous$$d of Camera.ScreenPointToRay
because i've read that it can happen that ScreenPointToRay doesn't work correct in the editor, but i haven't used it yet ;)
Your answer
Follow this Question
Related Questions
Unity Scene File in Build Question 1 Answer
Editor split scene 1 Answer
Executing first scene in build settings when pressing play button in editor? 2 Answers
Determine the Scene that is selected when using context menu 0 Answers
Editor Scene? 1 Answer