Beispiel #1
0
 private void FixedUpdate()
 {
     if (constantForceEnabled)
     {
         UnityFFBNative.UpdateConstantForce((int)(force * sensitivity), axisDirections);
     }
 }
Beispiel #2
0
        public void SelectDevice(string deviceGuid)
        {
#if UNITY_STANDALONE_WIN
            // For now just initialize the first FFB Device.
            int hresult = UnityFFBNative.CreateFFBDevice(deviceGuid);
            if (hresult == 0)
            {
                activeDevice = devices[0];

                if (disableAutoCenter)
                {
                    hresult = UnityFFBNative.SetAutoCenter(false);
                    if (hresult != 0)
                    {
                        Debug.LogError($"[UnityFFB] SetAutoCenter Failed: 0x{hresult.ToString("x")} {WinErrors.GetSystemMessage(hresult)}");
                    }
                }

                int    axisCount = 0;
                IntPtr ptrAxes   = UnityFFBNative.EnumerateFFBAxes(ref axisCount);
                if (axisCount > 0)
                {
                    axes             = new DeviceAxisInfo[axisCount];
                    axisDirections   = new int[axisCount];
                    springConditions = new DICondition[axisCount];

                    int axisSize = Marshal.SizeOf(typeof(DeviceAxisInfo));
                    for (int i = 0; i < axisCount; i++)
                    {
                        IntPtr pCurrent = ptrAxes + i * axisSize;
                        axes[i]             = Marshal.PtrToStructure <DeviceAxisInfo>(pCurrent);
                        axisDirections[i]   = 0;
                        springConditions[i] = new DICondition();
                    }

                    if (addConstantForce)
                    {
                        hresult = UnityFFBNative.AddFFBEffect(EffectsType.ConstantForce);
                        if (hresult == 0)
                        {
                            hresult = UnityFFBNative.UpdateConstantForce(0, axisDirections);
                            if (hresult != 0)
                            {
                                Debug.LogError($"[UnityFFB] UpdateConstantForce Failed: 0x{hresult.ToString("x")} {WinErrors.GetSystemMessage(hresult)}");
                            }
                            constantForceEnabled = true;
                        }
                        else
                        {
                            Debug.LogError($"[UnityFFB] AddConstantForce Failed: 0x{hresult.ToString("x")} {WinErrors.GetSystemMessage(hresult)}");
                        }
                    }

                    if (addSpringForce)
                    {
                        hresult = UnityFFBNative.AddFFBEffect(EffectsType.Spring);
                        if (hresult == 0)
                        {
                            for (int i = 0; i < springConditions.Length; i++)
                            {
                                springConditions[i].deadband            = 0;
                                springConditions[i].offset              = 0;
                                springConditions[i].negativeCoefficient = 2000;
                                springConditions[i].positiveCoefficient = 2000;
                                springConditions[i].negativeSaturation  = 10000;
                                springConditions[i].positiveSaturation  = 10000;
                            }
                            hresult = UnityFFBNative.UpdateSpring(springConditions);
                            Debug.LogError($"[UnityFFB] UpdateSpringForce Failed: 0x{hresult.ToString("x")} {WinErrors.GetSystemMessage(hresult)}");
                        }
                        else
                        {
                            Debug.LogError($"[UnityFFB] AddSpringForce Failed: 0x{hresult.ToString("x")} {WinErrors.GetSystemMessage(hresult)}");
                        }
                    }
                }
                Debug.Log($"[UnityFFB] Axis count: {axes.Length}");
                foreach (DeviceAxisInfo axis in axes)
                {
                    string ffbAxis = UnityEngine.JsonUtility.ToJson(axis, true);
                    Debug.Log(ffbAxis);
                }
            }
            else
            {
                activeDevice = null;
                Debug.LogError($"[UnityFFB] 0x{hresult.ToString("x")} {WinErrors.GetSystemMessage(hresult)}");
            }
#endif
        }