Get it on Google Play


Wm뮤 :: 'Unity/C#' 카테고리의 글 목록

블로그 이미지
불펌때문에 드래그 방지 건것가지고
99% 다 공유되는 지식 가져와서 쓰는 글 아니냐 누가 보면 자기 지식 훔쳐가는줄 자기도 다 베껴온거일텐데
같은 소리 할거면 꾸역꾸역 블로그 찾아오지 말고 AI나 쓰쇼.
세상에 없는게 많아서 손수 연구하고 정리해줬더니 이런 소리나 들어야 합니까?
모카쨩
@ahzkwid

Recent Comment

Archive


2025. 7. 3. 01:48 Unity/C#

 

먼저 구독관리를 해야 활성화 된다

 

 

 

 

 

1. 주석으로 코드 생성

 

주석으로 만들 코드를 설명하고 엔터치면 코드가 나온다

 

 

이 상태에서 TAP을 누르면 입력된다

 

 

 

 

 

2. 질문하기

 

 

 

 

질문 입력

 

 

해당되는 코드 입력

 

 

 

전송

 

 

근데 너무 빡대가리라 GPT 쓰는게 낫다

 

'Unity > C#' 카테고리의 다른 글

SharpPcap을 이용한 패킷분석 프로그램 제작  (0) 2025.06.24
유니티 오브젝트 경로 관련  (0) 2024.08.23
OnSceneGUI 관련코드  (0) 2024.07.11
posted by 모카쨩
2025. 6. 24. 16:15

보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.

2024. 8. 23. 06:30

보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.

2024. 7. 11. 19:30 Unity/C#

 

 

 

 

 

OnDrawGizmos처럼 상시동작하는점이 비슷하지만

OnDrawGizmos는 기즈모 드로우 옵션이 켜져있을때에만 동작하므로

기능코드는 가급적 OnSceneGUI에 넣는것이 좋다

 

 

 

1. Static에서 FindObjectsOfType를 이용해 가져와서 처리하는 방식

gist : https://gist.github.com/ahzkwid/3294e9d4cf3980ae2d1c135123db145c

public class SampleClass : MonoBehaviour
{
  static void OnSceneGUI(SceneView sceneView)
  {
      foreach (var sampleClass in FindObjectsOfType<SampleClass>())
      {
          if (sceneView.camera != null)
          {
              var sceneCamera = sceneView.camera;
              //CameraCode
          }
          //otherCode
      }
  }
}
//OtherSample: https://gist.github.com/ahzkwid/6647d3aec625afe96c54bb38b2671c72

 

 


2. SceneView.duringSceneGui에서 이벤트를 받는 방식

gist : https://gist.github.com/ahzkwid/315febfe26ef88adfa537bcfd6f6d49e

이 방식은 가볍지만 안정성은 1번에 비해서는 떨어진다

유니티 에디터 버그로 인해 Enable과 Disable이 제대로 호출 안 되거나 하는경우

이벤트가 중복호출되거나 등록 실패하는등...

일반적으로는 이 방식을 쓰고 무조건 동작해야 하는 너무 중요한 코드는 위방식을 쓰는게 좋다

[ExecuteInEditMode]
public class SampleClass : MonoBehaviour
{
    void OnEnable()
    {
        SceneView.duringSceneGui += OnSceneGUI;
    }

    void OnDisable()
    {
        SceneView.duringSceneGui -= OnSceneGUI;
    }
    void OnSceneGUI(SceneView sceneView)
    {
        //code
    }
}

 

 

 

 

 

 

 

 

3. EditorApplication.hierarchyWindowItemOnGUI 혼합예제

원문링크 : https://forum.unity.com/threads/solved-how-to-start-draganddrop-action-so-that-sceneview-and-hierarchy-accept-it-like-with-prefabs.822531/

아래 코드는 내가 만든게 아니고 위 링크에서 가져와 간소화 한것일뿐이다

[ExecuteInEditMode]
public class SampleClass : MonoBehaviour
{
     void OnEnable()
    {
        SceneView.duringSceneGui += OnSceneGUI;
        EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
    }

     void OnDisable()
    {
        SceneView.duringSceneGui -= OnSceneGUI;
        EditorApplication.hierarchyWindowItemOnGUI -= OnHierarchyGUI;
    }
     void OnSceneGUI(SceneView obj)
    {
        HandleDragAndDropEvents();
    }

     void OnHierarchyGUI(int instanceID, Rect selectionRect)
    {
        HandleDragAndDropEvents();
    }
     void HandleDragAndDropEvents()
    {
        if (Event.current.type == EventType.DragUpdated)
        {
            OnDragUpdated();
        }
        if (Event.current.type == EventType.DragPerform)
        {
            OnDragPerform();
        }
    }
    void OnDragUpdated()
    {
        Debug.Log("OnDragUpdated()");
    }
    void OnDragPerform()
    {
        Debug.Log("OnDragPerform()");
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

'Unity > C#' 카테고리의 다른 글

유니티 오브젝트 경로 관련  (0) 2024.08.23
유니티 라이트 레졸루션 개별설정  (0) 2024.05.12
유니티 프리팹 드래그 드랍  (0) 2024.05.09
posted by 모카쨩
2024. 5. 12. 12:01 Unity/C#

 

 

 

 

 

 

 

 

 

 

 

유니티 2022.3.22.f1 이후의 경우

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


유니티 2022.3.22.f1 이전의 경우는 아래 스크립트 적용 

 

 

 

SettingLightResolution.unitypackage
0.00MB

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteAlways]
public class SettingLightResolution : MonoBehaviour
{
    public Light light;
    public int resolution = 256;
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if (light==null)
        {
            return;
        }
        light.shadowCustomResolution = resolution;
#if UNITY_EDITOR
        UnityEditor.EditorUtility.SetDirty(light);
#endif
    }
}



어쩌다보니 만들게 됨

 

 

 

 

 

 

 

 

'Unity > C#' 카테고리의 다른 글

OnSceneGUI 관련코드  (0) 2024.07.11
유니티 프리팹 드래그 드랍  (0) 2024.05.09
AutomaticShadowDistance  (0) 2024.04.18
posted by 모카쨩
2024. 5. 9. 11:32 Unity/C#

 

 

 

 

원문링크 : 

https://forum.unity.com/threads/solved-how-to-start-draganddrop-action-so-that-sceneview-and-hierarchy-accept-it-like-with-prefabs.822531/

 

[Solved] How to start DragAndDrop action so that SceneView and Hierarchy accept it like with prefabs

Edit: Solution below. I'm building a level design tool which shows a list of prefabs in a custom editor window and lets users drag and drop these...

forum.unity.com

 

 

이 코드는 내가 만든게 아니고 위 링크에서 가져와 간소화 한것일뿐이다

 void OnEnable()
{
    SceneView.duringSceneGui += OnSceneGUI;
    EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}

 void OnDisable()
{
    SceneView.duringSceneGui -= OnSceneGUI;
    EditorApplication.hierarchyWindowItemOnGUI -= OnHierarchyGUI;
}
 void OnSceneGUI(SceneView obj)
{
    HandleDragAndDropEvents();
}

 void OnHierarchyGUI(int instanceID, Rect selectionRect)
{
    HandleDragAndDropEvents();
}
 void HandleDragAndDropEvents()
{
    if (Event.current.type == EventType.DragUpdated)
    {
        OnDragUpdated();
    }
    if (Event.current.type == EventType.DragPerform)
    {
        OnDragPerform();
    }
}
void OnDragUpdated()
{
    Debug.Log("OnDragUpdated()");
}
void OnDragPerform()
{
    Debug.Log("OnDragPerform()");
}

 

 

 

아바타 툴 만들때 사용되었다

 

 

 

 

 

'Unity > C#' 카테고리의 다른 글

유니티 라이트 레졸루션 개별설정  (0) 2024.05.12
AutomaticShadowDistance  (0) 2024.04.18
c# Dictionary(딕셔너리) 관련  (0) 2024.03.19
posted by 모카쨩
2024. 4. 18. 15:40 Unity/C#

 

 

 

https://ahzkwid.booth.pm/items/5632873

 

Automatic Shadow Distance - Wmup - BOOTH

ワールド用です DirectionalLightの影の長さをFPSに応じて自動的に変更するツールです。 動きがきれいじゃないので、キャラクターシャドウ専用に使用するのをお勧めします - Code - https://gist.githu

ahzkwid.booth.pm

여기서 다운 가능

 

 

 

 

 

using UdonSharp;
using UnityEngine;
using UnityEngine.UI;
using VRC.SDKBase;
using VRC.Udon;

[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class AutomaticShadowDistance : UdonSharpBehaviour
{
    public Light directionalLight;
    public float frameCheckTimeCycle = 3f;
    int playerLayerIndex = 9;
    public Vector2 targetFps = new Vector2(30, 60);
    public Vector2 defaultShadowDistance = new Vector2(10, 80);
    public Vector2 playerShadowDistance = new Vector2(1,15);

    float tPre = 2;

    void UpdateShadowDistance(float t)
    {
        var defaultShadowDistanceMin = defaultShadowDistance.x;
        var defaultShadowDistanceMax = defaultShadowDistance.y;
        var playerShadowDistanceMin = playerShadowDistance.x;
        var playerShadowDistanceMax = playerShadowDistance.y;


        var targetDistanceDefault = Mathf.Lerp(defaultShadowDistanceMin, defaultShadowDistanceMax, t);
        var targetDistancePlayer = Mathf.Lerp(playerShadowDistanceMin, playerShadowDistanceMax, t);
        
        {
            //QualitySettings.shadowDistance = targetDistanceDefault;
        }
        {
            var ShadowCullDistances = new float[32];
            //ShadowCullDistances[0] = Mathf.Lerp(ShadowCullDistances[0], targetDistanceDefault, 0.1f);
            ShadowCullDistances[0] = targetDistanceDefault;
            ShadowCullDistances[11] = ShadowCullDistances[0];
            ShadowCullDistances[13] = ShadowCullDistances[0];
            ShadowCullDistances[14] = ShadowCullDistances[0];
            ShadowCullDistances[playerLayerIndex] = targetDistancePlayer;
            ShadowCullDistances[10] = ShadowCullDistances[playerLayerIndex];
            ShadowCullDistances[18] = ShadowCullDistances[playerLayerIndex];
            directionalLight.layerShadowCullDistances = ShadowCullDistances;

            //Camera.main.layerCullDistances = ShadowCullDistances;
        }
    }

    LightShadows shadowType = LightShadows.Soft;
    void Start()
    {
        //if (directionalLight==null)
        //{
        //    /*
        //    var lights = FindObjectsOfType<Light>();
        //    foreach (var light in lights)
        //    {
        //        if (light.type==LightType.Directional)
        //        {
        //            directionalLight = light;
        //        }
        //    }
        //    */
        //    /*
        //    var light= FindObjectOfType<Light>();
        //    if (light.type == LightType.Directional)
        //    {
        //        directionalLight = light;
        //    }
        //    */
        //}
        if (directionalLight.shadows != LightShadows.None)
        {
            shadowType = directionalLight.shadows;
        }

        UpdateShadowDistance(0);

        lastCheckTime = 0;
    }
    float lastCheckTime = 0;
    int frameCount = 0;

    public Toggle toggle;
    public Slider slider;
    public GameObject lockPanel;

    public bool alwaysUseShdow = true;
    bool CheckT(float t)
    {
        return ((Mathf.Abs(t - tPre) > 0.1f)
            || ((t == 0) && (tPre < 0.1f))
            || ((t == 1) && (tPre > 0.9f)));
    }
    void Update()
    {
        var useShadowOff = !alwaysUseShdow;

        var toggleManual = toggle;
        lockPanel.SetActive(toggleManual.isOn == false);



        var useShadowOffvalue = 0.1f;
        if (toggleManual.isOn)
        {
            if (slider.value > useShadowOffvalue)
            {
                var t = (slider.value - useShadowOffvalue) * (1f / (1f- useShadowOffvalue));
                if (CheckT(t))
                {
                    UpdateShadowDistance(t);
                    tPre = t;
                }
                if (useShadowOff)
                {
                    if (directionalLight.shadows == LightShadows.None)
                    {
                        directionalLight.shadows = shadowType;
                    }
                }
            }
            else
            {
                if (useShadowOff)
                {
                    if (directionalLight.shadows != LightShadows.None)
                    {
                        directionalLight.shadows = LightShadows.None;
                    }
                }
            }
        }
        else
        {


            frameCount++;

            if (Time.time - lastCheckTime >= frameCheckTimeCycle)
            {
                var fps = 0f;
                if (Time.deltaTime > 0)
                {
                    fps = 1f / Time.deltaTime;
                }
                if (frameCheckTimeCycle > 0.1f)
                {
                    fps = frameCount / frameCheckTimeCycle;
                }


                var min = targetFps.x;
                var max = targetFps.y;
                var t = (Mathf.Clamp(fps, min, max) - min) / (max - min);

                /*
                if ((Mathf.Abs(t - tPre) > 0.1f)
                    || ((t == 0) && (tPre < 0.1f))
                    || ((t == 1) && (tPre > 0.9f)))
                */
                if (CheckT(t))
                {

                    UpdateShadowDistance(t);
                    tPre = t;

                }


                if (Time.time > 10)
                {
                    if (useShadowOff)
                    {
                        if (directionalLight.shadows == LightShadows.None)
                        {
                            if (t > 0.8f)
                            {
                                directionalLight.shadows = shadowType;
                            }
                        }
                        else
                        {
                            if (fps < min / 2)
                            {
                                directionalLight.shadows = LightShadows.None;
                            }
                        }
                    }
                }
                if (directionalLight.shadows == LightShadows.None)
                {
                    slider.value = 0f;
                }
                else
                {
                    slider.value = useShadowOffvalue + t * (1f- useShadowOffvalue);
                }


                frameCount = 0;
                lastCheckTime = Time.time;
            }
        }
    }
}

'Unity > C#' 카테고리의 다른 글

유니티 프리팹 드래그 드랍  (0) 2024.05.09
c# Dictionary(딕셔너리) 관련  (0) 2024.03.19
자주 쓰는 DateTime 코드 모음  (0) 2023.12.19
posted by 모카쨩
2024. 3. 19. 01:48

보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.