unity工具——延迟回调

一个实用的计时器,可以计时延迟调用和延迟重复次数调用。
可以自己封装成单例模式挂在GameObject上使用,或者在另一个behavior的Update里执行这个类的OnUpdate()方法再使用。
为了更加安全的使用,建议在销毁MonoBehaviour时清理一下对应的所有计时器。
或者调用时可选择传入回调所在的MonoBehaviour,这样就可以自动清理了。

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

public static class DelayCall
{

    private static List<CallTimeObj> calltimes = new List<CallTimeObj>();
    private static Dictionary<int, CallObj> callsort = new Dictionary<int, CallObj>();
    private static int countid = 0;
    /// <summary>
    /// 生成id
    /// </summary>
    /// <returns>The new identifier.</returns>
    /// <param name="call">Call.</param>
    private static int getNewId(CallObj call)
    {
        countid++;
        if (countid >= int.MaxValue)
        {
            countid = 1;
        }
        while (callsort.ContainsKey(countid)) countid++;
        call.callid = countid;
        callsort.Add(countid, call);
        return countid;

    }
    public static
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Unity的ReorderableList中实现鼠标右键回调,可以使用UnityEditor.GenericMenu类。首先,在ReorderableList的OnGUI函数中,检测是否有鼠标右键按下事件,如果有,则创建一个GenericMenu对象,并添加菜单项。然后,调用GenericMenu的ShowAsContext函数,以显示右键菜单。 以下是一个示例代码,用于在ReorderableList中实现鼠标右键回调: ```csharp using UnityEditor; using UnityEditorInternal; using UnityEngine; public class Example : EditorWindow { private ReorderableList list; [MenuItem("Window/Example")] private static void ShowWindow() { GetWindow<Example>("Example"); } private void OnEnable() { list = new ReorderableList(serializedObject, serializedObject.FindProperty("list"), true, true, true, true); list.drawElementCallback = (rect, index, isActive, isFocused) => { var element = list.serializedProperty.GetArrayElementAtIndex(index); rect.y += 2; EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element); }; list.drawHeaderCallback = rect => { EditorGUI.LabelField(rect, "List"); }; list.onAddCallback = list => { var index = list.serializedProperty.arraySize; list.serializedProperty.arraySize++; list.index = index; }; list.onRemoveCallback = list => { if (EditorUtility.DisplayDialog("Delete item", "Are you sure?", "Yes", "No")) { ReorderableList.defaultBehaviours.DoRemoveButton(list); } }; list.onMouseUpCallback = list => { if (Event.current.button == 1 && list.index >= 0 && list.index < list.serializedProperty.arraySize) { var menu = new GenericMenu(); menu.AddItem(new GUIContent("Option 1"), false, () => { Debug.Log("Option 1 selected"); }); menu.AddItem(new GUIContent("Option 2"), false, () => { Debug.Log("Option 2 selected"); }); menu.ShowAsContext(); Event.current.Use(); } }; } private void OnGUI() { serializedObject.Update(); list.DoLayoutList(); serializedObject.ApplyModifiedProperties(); } } ``` 在这个示例中,我们在ReorderableList的onMouseUpCallback函数中检测鼠标右键按下事件,并创建一个带有两个菜单项的GenericMenu对象。当菜单项被选中时,会调用相应的回调函数。在这个示例中,我们只是简单地记录了选中的菜单项,但你可以根据需要执行任何操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值