c# good code examples

Example 1: c# code examples

using System;

#region Studio Style
class Program : IThemeable
{
    static int _I = 1;
    delegate void DoSomething();

    /// <summary>
    /// The quick brown fox jumps over the lazy dog
    /// THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
    /// </summary>
    static void Main(string[] args)
    {
        string normalStr = "The time now is approximately " + DateTime.Now;
        Uri Illegal1Uri = new Uri("http://packmyboxwith/jugs.html?q=five-dozen&t=liquor");
        Regex OperatorRegex = new Regex(@"\S#$", RegexOptions.IgnorePatternWhitespace);

        for (int O = 0; O < 123456789; O++)
        {
            _I += (O % 3) * ((O / 1) ^ 2) - 5;
            if (!OperatorRegex.IsMatch(Illegal1Uri.ToString()))
            {
                // no idea what this does!?
                Console.WriteLine(Illegal1Uri + normalStr);

            }
        }
    }
}
#endregion

Example 2: c# code examples

IEnumerator ExampleCoroutine()//jump
		{
			yield return new WaitForSeconds(1.5f);
		}

Example 3: c# code examples

//https://www.youtube.com/watch?v=rDJOilo4Xrg
using System.Collections;
using MLAPI;
using MLAPI.Messaging;
using MLAPI.NetworkVariable;
using UnityEngine;

namespace AnotherUniverse
{
    public class AnotherCameraOrbit : NetworkBehaviour
    {
        [SerializeField] private Camera cam;
        //[SerializeField] private Transform target;
        [SerializeField] private float distanceToTarget = 10;

        

        [SerializeField] private float mouseSensitivity;//copied from my other project scripts

        void Start()
        {
            //Cursor.lockState = CursorLockMode.Locked;//copied from my other project scripts
        }

        void Update()
        {
            if (!IsOwner) { return; }
            CamOrbServerRpc();
            
        }

        [ServerRpc]
        private void CamOrbServerRpc()
        {
            CamOrbClientRpc();
        }

        [ClientRpc]
        private void CamOrbClientRpc()
        {
            float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;//copied from my other project scripts
            float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;//copied from my other project scripts

            float rotationAroundYAxis = mouseX * 180; // camera moves horizontally
            float rotationAroundXAxis = -mouseY * 180; // camera moves vertically

            //I use this because if I use the serializefield, the prefab will then clone a new different one and the target.position will be useless----------------------
            if (NetworkManager.Singleton.ConnectedClients.TryGetValue(NetworkManager.Singleton.LocalClientId,
                    out var networkedClient))//declaring the user as networkedclient
                {
                    var player = networkedClient.PlayerObject.GetComponent<AnotherMyPlayer>();
                    if (player)
                    {
                        cam.transform.position = player.transform.position;//cam.transform.position = target.position;
                    }
                }
            string bruh = mouseX.ToString();
            print(bruh);
                //------------------------------------------------------------------------------------------------------------------------------------------------------------

                cam.transform.Rotate(new Vector3(1, 0, 0), rotationAroundXAxis);
            cam.transform.Rotate(new Vector3(0, 1, 0), rotationAroundYAxis, Space.World); // <— This is what makes it work!

            cam.transform.Translate(new Vector3(0, 0, -distanceToTarget));
            //print("Camera is working");
        }
    }
}







/*THIS SCRIPT ROTATES CAMERA AROUND THE OBJECT/PLAYER WHEN MOUSE IS HOLD-CLICK, CAN BE USEFUL FOR INSPECTING WEAPONS OR CHARACTERS
using UnityEngine;
using System.Collections;

public class CameraOrbit : MonoBehaviour
{

    public float turnSpeed = 4.0f;
    public Transform player;

    private Vector3 offset;

    void Start()
    {
        offset = new Vector3(player.position.x, player.position.y + 8.0f, player.position.z + 7.0f);
    }

    void LateUpdate()
    {
        transform.position = player.position + offset;
        transform.LookAt(player.position);


        if (Input.GetMouseButton(1))
        {
            offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
        }
    }

}
*/