Skip to content

Instantly share code, notes, and snippets.

View starikcetin's full-sized avatar

S. Tarık Çetin starikcetin

View GitHub Profile
@starikcetin
starikcetin / ParentAwareContentSizeFitter.cs
Created July 24, 2024 12:52
Modified version of Unity's ContentSizeFitter to allow filling the parent if the content ends up smaller.
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine;
using System.Collections.Generic;
[AddComponentMenu("Layout/Parent Aware Content Size Fitter", 141)]
[ExecuteAlways]
[RequireComponent(typeof(RectTransform))]
/// <summary>
/// Resizes a RectTransform to fit the size of its content, while also allowing to fill the parent if the content is smaller.
@starikcetin
starikcetin / Copty C# DLL to somewhere after build.md
Created February 6, 2024 13:30
Copty C# DLL to somewhere after build

Add this to .csproj:

  <Target Name="CopyDLL" AfterTargets="Build">
    <Message Text="Copying DLL" Importance="High" />
    <Copy
      SourceFiles="$(TargetDir)$(ProjectName).dll"
      DestinationFolder="$(MSBuildProjectDirectory)\relative_target_folder_path_here\"
    />
  </Target>
@starikcetin
starikcetin / Running Code at Unity Editor Launch Before Scripts Compile.md
Last active March 6, 2024 13:16
Running Code at Unity Editor Launch Before Scripts Compile

Create a standalone pure C# project.

Create a Directory.Build.props right alongside your .csproj file:

<Project>
    <PropertyGroup>
        <UnityProjectPath>$(MSBuildProjectDirectory)\relative_path_to_unity_project\</UnityProjectPath>
    </PropertyGroup>
</Project>
#!/bin/bash
shopt -s globstar
FILES=(./**/*.*)
TOTAL_COUNT=${#FILES[@]}
CURRENT_COUNT=1
for i in "${FILES[@]}"; do
echo "processing (${CURRENT_COUNT} of ${TOTAL_COUNT}) ${i}"
@starikcetin
starikcetin / GuidTools.cs
Created June 11, 2023 18:19
Unity Project Window GUID Tools
using System.Text;
using UnityEditor;
using UnityEngine;
public static class GuidTools
{
[MenuItem("Assets/GUID Tools/Log GUIDs")]
private static void LogGuids()
{
var guids = Selection.assetGUIDs;
@starikcetin
starikcetin / EditorUtils.cs
Last active November 20, 2023 20:16
Unity get Attributes of a specific type on a SerializedProperty
using System;
using System.Reflection;
using UnityEditor;
public static class EditorUtils
{
private const BindingFlags AllBindingFlags = (BindingFlags)(-1);
/// <summary>
/// Returns attributes of type <typeparamref name="TAttribute"/> on <paramref name="serializedProperty"/>.
@starikcetin
starikcetin / SceneExtensions.cs
Last active July 27, 2022 16:41
SceneUtils, GetAllRootGameObjects, DontDestroyOnLoadScene, etc.
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
public static class SceneExtensions
{
/// <summary>
/// Returns whether the given scene contains the given component on any of its GameObjects.
/// </summary>
public static bool HasComponent<T>(this Scene scene)
@starikcetin
starikcetin / AssetDefinePostprocessor.cs
Created July 8, 2022 16:34 — forked from nonathaj/AssetDefinePostprocessor.cs
A script for creating and removing defines in Unity with C# along with certain files.
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
[InitializeOnLoad]
public class AssetDefinePostprocessor : AssetPostprocessor
{
@starikcetin
starikcetin / MainThreadDispatcher.cs
Last active July 27, 2022 16:45
Main thread dispatcher for Unity.
using System;
using System.Threading;
// TODO: Make sure Singleton implementation is also thread-safe.
/// <summary>
/// Provides means of executing code on the main Unity thread while in other threads.
/// </summary>
public class MainThreadDispatcher
{
@starikcetin
starikcetin / CanvasPositioningExtensions.cs
Created August 19, 2021 18:53 — forked from FlaShG/CanvasPositioningExtensions.cs
A small Unity helper class to convert viewport, screen or world positions to canvas space.
using UnityEngine;
/// <summary>
/// Small helper class to convert viewport, screen or world positions to canvas space.
/// Only works with screen space canvases.
/// </summary>
/// <example>
/// <code>
/// objectOnCanvasRectTransform.anchoredPosition = specificCanvas.WorldToCanvasPoint(worldspaceTransform.position);
/// </code>