Code Samples

Turret.cs (From C.W.I.S)

Language: C#

The turret base class used by the weapons on the ship in C.W.I.S, this controls the turret rotation towards the mouse & the object pooling for the bullets/missiles in the game.

Sample:

        /// ------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Rotates the turret to the mouse position.
        /// </summary>
        /// <param name="_min">Min rotation</param>
        /// <param name="_max">Max rotation</param>
        /// <returns>Quaternion rotation for the object</returns>
        /// ------------------------------------------------------------------------------------------------------
        internal Quaternion RotateToMousePos(float offset = 0)
        {
            InputSystem.onActionChange += (obj, change) =>
            {
                if (change == InputActionChange.ActionPerformed)
                {
                    var inputAction = (InputAction)obj;
                    var lastControl = inputAction.activeControl;
                    device = lastControl.device;
                }
            };

            if (device != null)
            {
                if (device.displayName.Equals("Mouse"))
                {
                    Ray ray = cam.ScreenPointToRay(actions.Weapons.Position.ReadValue<Vector2>());
                    Plane plane = new Plane(Vector3.up, Vector3.zero);
                    float distance;
                    float rotation = 0;

                    if (plane.Raycast(ray, out distance))
                    {
                        Vector3 target = ray.GetPoint(distance);
                        Vector3 direction = target - transform.position;
                        rotation = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + 180f;
                    }

                    return Quaternion.Euler(0, rotation, 0);
                }
                else
                {
                    return Quaternion.Euler(0, Mathf.Atan2(actions.Weapons.PositionJoystick.ReadValue<Vector2>().x, actions.Weapons.PositionJoystick.ReadValue<Vector2>().y) * Mathf.Rad2Deg, 0);
                }
            }
            else
            {
                return Quaternion.Euler(0, 0, 0);
            }
        }

UpdateCardVisuals Method (From Detective Notes)

Language: C#

The method that edits the visuals when a user selects an icon from the “editing” popup. This method uses elements of the CarterGames.Utilities Library instead of Int.Parse and ToString().???

Sample:

if (!StringHelper.Get(1, status).Equals(0))
                    {
                        _icons[0].SetActive(false);
                        _icons[1].SetActive(true);
                        _icons[1].transform.GetChild(0).GetComponentsInChildren<Image>()[0].color = _scm.elements.colors[As.Int(StringHelper.Get(0, status)) - 1];
                        _icons[1].transform.GetChild(0).GetComponentsInChildren<Image>()[1].sprite = _scm.elements.icons[As.Int(StringHelper.Get(0, status)) - 1];
                        _icons[1].transform.GetChild(0).GetComponentsInChildren<Image>()[1].enabled = true;
                        _icons[1].transform.GetChild(1).GetComponentsInChildren<Image>()[0].color = _scm.elements.colors[As.Int(StringHelper.Get(1, status)) - 1];
                        _icons[1].transform.GetChild(1).GetComponentsInChildren<Image>()[1].sprite = _scm.elements.icons[As.Int(StringHelper.Get(1, status)) - 1];
                        _icons[1].transform.GetChild(1).GetComponentsInChildren<Image>()[1].enabled = true;
                    }

TabOneDisplay Method (From Save Manager)

Language: C#

The code the runs the display in the first tab of the Save Data Editor in the Save manager asset. It’s purpose is to help the user name a class with savable values.

Sample:

            GUI.backgroundColor = Color.green;
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("+ Add Field", GUILayout.Width(90f)))
            {
                if (!isCreatingFile)
                {
                    isCreatingFile = true;
                    types = new List<dataTypes>();
                    shouldArray = new List<bool>();
                    shouldList = new List<bool>();
                    valueNames = new List<string>();
                    classNames = new List<string>();
                    types.Add(dataTypes.stringValue);
                    shouldArray.Add(false);
                    shouldList.Add(false);
                    valueNames.Add("");
                    classNames.Add("");
                }
                else
                {
                    types.Add(dataTypes.stringValue);
                    shouldArray.Add(false);
                    shouldList.Add(false);
                    valueNames.Add("");
                    classNames.Add("");
                }
            }
            GUI.backgroundColor = Color.white;
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();