- Home /
SetCursorPos reset when reloading scene
hello i am using
DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int SetCursorPos(int x,int y);
to take control of the mouse by using an eyetracker....Everything works fine.....but...after i die in the game, or reload the scene..... the static setcursorpos doesn`t reinitialize....i tried singleton or other suggested approaches in the unity answers....but it still doesn`t work? Do you have any other sugestions how to solve the problem? I am working on a windows powered tablet....that has mouse input...Instead of mouse can i use Raycast in order to determine if my eyetracker coordinates are over a gameobject`s collider?
When you made it a singleton did you use DontDestroyOnLoad? http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html
yes...can you maybe elaborate a little bit on a method how to do it properly? I `ve tried lots of methods for the last past weeks, and none of those is working properly.....The thing is that i get x and y screen coordinates from the eyetracker, which i then replace this coordinates in an update function in SetCursorPos(gazex,gazey).....working really good..undless i restart the scene..when it stops working....I am using it to check if the mouse is over an enemy, and then renders a HUD target over it, and changes it`s tag to enemy, so the rocket seeker....shots it....Would there be a better method to have this on hover effect without using and taking control of the mouse? Your help is highly apreciated
maybe something like this:
http://forum.unity3d.com/threads/64880-Troubles-with-raycasting-to-find-tags
where the point of shooting the ray is a Vector2 that holds the eyetracker x, y coordinates
Answer by PAEvenson · Nov 29, 2012 at 01:36 PM
Here try something like this:
using UnityEngine;
using System.Collections;
public class MouseManager : MonoBehaviour
{
private static MouseManager m_Instance = null;
public static MouseManager Instance
{
get
{
if(m_Instance == null)
{
m_Instance = (GameObject.Instantiate(Resources.Load("Prefabs/MouseManager"))as GameObject).GetComponent<MouseManager>();
}
return m_Instance;
}
}
void Awake()
{
GameObject.DontDestroyOnLoad (gameObject);
}
//place your code here
//
}
You will need to make a prefab and place it in your resources folder and attach the script to it. DO NOT add the prefab to the scene. It will load itself when you try to access it. I havent tested the code..there maybe a bug or 2, but should be a good start for you.
I've had issues in the past when I reloaded a scene with a plugin that used a dll. I dont think it was cleaning up properly and didnt release the windows handle. Hope this helps
".....working really good..undless i restart the scene..when it stops working...." I think the issue is with the dll and windows handle, not your eyetracking...
Thank you...$$anonymous$$y script was attached to the camera...so you suggest i create an empty prefab, that i don`t use in the scene? cause my eyetracking code was in the same script as well...so i want to make sure it works
The only thing that needs to be in the prefab is the DLL import stuff. I would then change all my SetCursorPos calls to use $$anonymous$$ouse$$anonymous$$anager.Instance.SetCursorPos(). That way all you need to do is a "Find" in $$anonymous$$onoDevelop for SetCursorPos and replace it with $$anonymous$$ouse$$anonymous$$anager.Instance.SetCursorPos or whatever you call it.
Yea, if you had the DLL import stuff on your $$anonymous$$ainCamera, when the scene reloaded you probably had 2 mainCamera in the scene now. Then, the DLL Import got recalled again breaking the windows handler.
wait what exactly doesnt work? When you restart the scene is it still broken? Or does it not work on the tablet?
Your answer
Follow this Question
Related Questions
Attack/Targeting Script Issue 1 Answer
Throw with mouse 1 Answer
Variable Accessors in UnityScript (Get{} and Set{}) 1 Answer
Static target in the srcipt for my Prefab 2 Answers
Singleton instance accessed in coroutine always null 1 Answer