監(jiān)聽資源導(dǎo)入接口(AssetPostprocessor)淑仆,批量修改新增圖片資源設(shè)置
public class ImportAsset : AssetPostprocessor
{
? ? ? ? /// <summary>
? ? /// 紋理導(dǎo)入之后調(diào)用
? ? /// </summary>
? ? static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
? ? {
? ? }
? ? ? /// <summary>
? ? /// 紋理導(dǎo)入之前調(diào)用
? ? /// </summary>
? ? void OnPreprocessTexture()
? ? {
? ? }
? ? /// <summary>
? ? /// 音頻資源導(dǎo)入完成之前調(diào)用
? ? /// </summary>
? ? private void OnPreprocessAudio()
? ? {
? ? ? ? AudioImporter _importer = (AudioImporter)assetImporter;
? ? ? ? _importer.preloadAudioData = true;
? ? }
? ? /// <summary>
? ? /// 從模型(.fbx隧膘,.mb文件等)導(dǎo)入動畫之前調(diào)用
? ? /// </summary>
? ? private void OnPreprocessAnimation()
? ? {
? ? ? ? ModelImporter _importer = (ModelImporter)assetImporter;
? ? }
? ? /// <summary>
? ? /// 模型導(dǎo)入之前調(diào)用
? ? /// </summary>
? ? private void OnPreprocessModel()
? ? {
? ? ? ? ModelImporter _importer = (ModelImporter)assetImporter;
? ? }
? ? /// <summary>
? ? /// 音頻資源導(dǎo)入完成之后調(diào)用
? ? /// </summary>
? ? /// <param name="clip"></param>
? ? private void OnPostprocessAudio(AudioClip clip)
? ? {
? ? ? ? Debug.Log("導(dǎo)入音頻:" + clip.name);
? ? ? ? AudioImporter _importer = (AudioImporter)assetImporter;
? ? }
? ? /// <summary>
? ? /// 模型導(dǎo)入完成之后調(diào)用
? ? /// </summary>
? ? /// <param name="g"></param>
? ? private void OnPostprocessModel(GameObject g)
? ? {
? ? ? ? Debug.Log("導(dǎo)入模型:" + g.name);
? ? }
? ? /// <summary>
? ? /// 精靈的紋理導(dǎo)入完成之后調(diào)用
? ? /// </summary>
? ? /// <param name="texture"></param>
? ? /// <param name="sprites"></param>
? ? private void OnPostprocessSprites(Texture2D texture, Sprite[] sprites)
? ? {
? ? ? ? Debug.Log("導(dǎo)入紋理:" + texture.name);
? ? }
}
下面是我實現(xiàn)的類币旧,參考了網(wǎng)絡(luò)上的文章
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if? UNITY_ANDROID
using System;
using System.Collections.Generic;
using Unity.Notifications.Android;
using UnityEngine;
public class AndroidNotificationSender : MonoBehaviour
{
? ? private static bool _isInitialized;
? ? private static List<NotificationInfo> _notificationInfos;
? ? private static int _notificationId = 1;
? ? private static void Init()
? ? {
? ? ? ? if (_isInitialized)
? ? ? ? ? ? return;
? ? ? ? _notificationInfos = new List<NotificationInfo>();
? ? ? ? ResetNotificationChannel();
? ? ? ? var notificationGo = new GameObject("NotificationBehaviour").AddComponent<NotificationSender>();
? ? ? ? DontDestroyOnLoad(notificationGo);
? ? ? ? _isInitialized = true;
? ? }
? ? private static void ResetNotificationChannel()
? ? {
? ? ? ? _notificationId = 1;
? ? ? ? AndroidNotificationCenter.CancelAllNotifications();//清除上次注冊的通知
? ? ? ? var channel = new AndroidNotificationChannel()
? ? ? ? {
? ? ? ? ? ? Id = "channel_id",
? ? ? ? ? ? Name = "Default Channel",
? ? ? ? ? ? Importance = Importance.High,
? ? ? ? ? ? Description = "Generic notifications",
? ? ? ? ? ? CanShowBadge = true,
? ? ? ? ? ? EnableLights = true,
? ? ? ? ? ? LockScreenVisibility = LockScreenVisibility.Public
? ? ? ? };
? ? ? ? AndroidNotificationCenter.RegisterNotificationChannel(channel);
? ? }
? ? protected static void ReSendNotification()
? ? {
? ? ? ? if (_isInitialized && _notificationInfos != null && _notificationInfos.Count > 0)
? ? ? ? {
? ? ? ? ? ? ResetNotificationChannel();
? ? ? ? ? ? for (var i = 0; i < _notificationInfos.Count; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? SendNotification(_notificationInfos[i]);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public static void SendNotification(string title, string text, int day, int hour, int minute, int second, string smallIconId = null, string largeIconId = null)
? ? {
? ? ? ? Init();
? ? ? ? var notificationInfo = new NotificationInfo()
? ? ? ? {
? ? ? ? ? ? title = title,
? ? ? ? ? ? text = text,
? ? ? ? ? ? day = day,
? ? ? ? ? ? hour = hour,
? ? ? ? ? ? minute = minute,
? ? ? ? ? ? second = second,
? ? ? ? ? ? smallIcon = smallIconId,
? ? ? ? ? ? largeIcon = largeIconId
? ? ? ? };
? ? ? ? _notificationInfos.Add(notificationInfo);
? ? ? ? SendNotification(notificationInfo);
? ? }
? ? public static void SendRepeatNotification(string title,string text)
? ? {
? ? ? ? Init();
? ? ? ? DateTime now = DateTime.Now;
? ? ? ? AndroidNotification notification = new AndroidNotification
? ? ? ? {
? ? ? ? ? ? Title = title,
? ? ? ? ? ? Text = text,
? ? ? ? ? ? FireTime = new DateTime(now.Year, now.Month, now.Day, 12, 0, 0).AddDays(1),
? ? ? ? ? ? RepeatInterval = new TimeSpan(1, 0, 0, 0),
? ? ? ? ? ? IntentData = "{\"title\": \"Notification 1\", \"data\": \"200\"}",
? ? ? ? ? ? ShouldAutoCancel = true
? ? ? ? };
? ? ? ? AndroidNotificationCenter.SendNotification(notification, "channel_id");
? ? }
? ? private static void SendNotification(NotificationInfo notificationInfo)//string title, string text,DateTime time,string smallIconId=null,string largeIconId=null)
? ? {
? ? ? ? DateTime now = DateTime.Now;
? ? ? ? var time = NotificationSender.GetNotificationTime(notificationInfo);
? ? ? ? var notification = new AndroidNotification()
? ? ? ? {
? ? ? ? ? ? Title = notificationInfo.title,
? ? ? ? ? ? Text = notificationInfo.text,
? ? ? ? ? ? FireTime = time,
? ? ? ? ? ? SmallIcon = notificationInfo.smallIcon,
? ? ? ? ? ? LargeIcon = notificationInfo.largeIcon,
? ? ? ? ? ? Number = _notificationId
? ? ? ? };
? ? ? ? _notificationId++;
? ? ? ? AndroidNotificationCenter.SendNotification(notification, "channel_id");
? ? }
}
#endif
#if? UNITY_IOS
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Notifications.iOS;
public class iOSNotificationSender:MonoBehaviour
{
? ? private static bool _isInitialized = false;
? ? private static int _notificationId = 1;
? ? private static List<NotificationInfo> _notificationInfos;
? ? // Start is called before the first frame update
? ? private static void Init()
? ? {
? ? ? ? if(_isInitialized)
? ? ? ? ? ? return;
? ? ? ? _notificationInfos = new List<NotificationInfo>();
? ? ? ? ResetNotificationChannel();
? ? ? ? var notificationGo= new GameObject("NotificationBehaviour").AddComponent<NotificationSender>();
? ? ? ? DontDestroyOnLoad(notificationGo);
? ? ? ? _isInitialized = true;
? ? }
? ? private static void ResetNotificationChannel()
? ? {
? ? ? ? _notificationId = 1;
? ? ? ? iOSNotificationCenter.ApplicationBadge=0;
? ? ? ? iOSNotificationCenter.RemoveAllDeliveredNotifications();
? ? ? ? iOSNotificationCenter.RemoveAllScheduledNotifications();
? ? }
? ? protected static void ReSendNotification()
? ? {
? ? ? ? if (_isInitialized&&_notificationInfos!=null && _notificationInfos.Count > 0)
? ? ? ? {
? ? ? ? ? ? ResetNotificationChannel();
? ? ? ? ? ? for (var i = 0; i < _notificationInfos.Count; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? SendNotification(_notificationInfos[i]);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public static void SendNotification(string title, string text,int day,int hour,int minute,int second ,string smallIconId=null,string largeIconId=null)
? ? {
? ? ? ? Init();
? ? ? ? var notificationInfo = new NotificationInfo()
? ? ? ? {
? ? ? ? ? ? title = title,
? ? ? ? ? ? text=text,
? ? ? ? ? ? day=day,
? ? ? ? ? ? hour = hour,
? ? ? ? ? ? minute = minute,
? ? ? ? ? ? second = second,
? ? ? ? };
? ? ? ? _notificationInfos.Add(notificationInfo);
? ? ? ? SendNotification(notificationInfo);
? ? }
? ? private static void SendNotification(NotificationInfo notificationInfo)//string title, string text,TimeSpan timeInterval)
? ? {
? ? ? ? var time = NotificationSender.GetNotificationTime(notificationInfo);
? ? ? ? var timeInterval =time.Subtract(DateTime.Now);
? ? ? ? var timeTrigger = new iOSNotificationTimeIntervalTrigger()
? ? ? ? {
? ? ? ? ? ? TimeInterval = new TimeSpan(timeInterval.Days, timeInterval.Hours, timeInterval.Minutes, timeInterval.Seconds),// timeInterval,
//? ? ? ? ? ? TimeInterval = new TimeSpan(0,0,0,5),// timeInterval,
? ? ? ? ? ? Repeats = false
? ? ? ? };
? ? ? ? var notification = new iOSNotification()
? ? ? ? {
? ? ? ? ? ? // You can optionally specify a custom identifier which can later be
? ? ? ? ? ? // used to cancel the notification, if you don't set one, a unique
? ? ? ? ? ? // string will be generated automatically.
? ? ? ? ? ? Identifier = "_notification_"+ _notificationId,
? ? ? ? ? ? Title = notificationInfo.title,
? ? ? ? ? ? Body = notificationInfo.text,
? ? ? ? ? ? Badge = _notificationId,
? ? ? ? ? ? ShowInForeground = true,
? ? ? ? ? ? ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound | PresentationOption.Badge),
? ? ? ? ? ? CategoryIdentifier = "category_a",
? ? ? ? ? ? ThreadIdentifier = "thread1",
? ? ? ? ? ? Trigger = timeTrigger,
? ? ? ? };
? ? ? ? _notificationId++;
? ? ? ? iOSNotificationCenter.ScheduleNotification(notification);
? ? }
}
#endif
public class NotificationInfo
{
? ? public string title;
? ? public string text;
? ? public int day;
? ? public int hour;
? ? public int minute;
? ? public int second;
? ? public string smallIcon;
? ? public string largeIcon;
}
public class NotificationSender :
#if UNITY_ANDROID
? ? AndroidNotificationSender
#else
? ? iOSNotificationSender
#endif
{
? ? private void OnApplicationFocus(bool hasFocus)
? ? {
? ? ? ? if (hasFocus)
? ? ? ? {
? ? ? ? ? ? ReSendNotification();
? ? ? ? ? ? SendRepeatNotification(string.Empty, "Daily rewards can be claimed轻庆!");
? ? ? ? }
? ? }
? ? /// <summary>
? ? /// 得到注冊通知的時間
? ? /// </summary>
? ? /// <returns></returns>
? ? public static DateTime GetNotificationTime(NotificationInfo notificationInfo)
? ? {
? ? ? ? var daySpan = new TimeSpan(notificationInfo.day, 0, 0, 0);
? ? ? ? var dateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, notificationInfo.hour, notificationInfo.minute, notificationInfo.second);
? ? ? ? dateTime += daySpan;
? ? ? ? Debug.Log("RegisterNotification:" + dateTime);
? ? ? ? //if (dateTime.Hour >= 23)
? ? ? ? //{
? ? ? ? //? ? dateTime += new TimeSpan(9, 0, 0);
? ? ? ? //}
? ? ? ? //if (dateTime.Hour <= 8)
? ? ? ? //{
? ? ? ? //? ? dateTime += new TimeSpan(8 - dateTime.Hour, 0, 0);
? ? ? ? //}
? ? ? ? // Debug.Log("LinneaNotify 發(fā)出通知時間 : " + dateTime);
? ? ? ? return dateTime;
? ? }
}