unity 監(jiān)聽導(dǎo)入資源

監(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;

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蚕礼,隨后出現(xiàn)的幾起案子叶圃,更是在濱河造成了極大的恐慌,老刑警劉巖织盼,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件杨何,死亡現(xiàn)場離奇詭異,居然都是意外死亡沥邻,警方通過查閱死者的電腦和手機危虱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來唐全,“玉大人埃跷,你說我怎么就攤上這事∮世” “怎么了弥雹?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長延届。 經(jīng)常有香客問我剪勿,道長,這世上最難降的妖魔是什么方庭? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任厕吉,我火速辦了婚禮,結(jié)果婚禮上械念,老公的妹妹穿的比我還像新娘头朱。我一直安慰自己,他們只是感情好龄减,可當(dāng)我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布项钮。 她就那樣靜靜地躺著,像睡著了一般希停。 火紅的嫁衣襯著肌膚如雪烁巫。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天宠能,我揣著相機與錄音亚隙,去河邊找鬼。 笑死棍潘,一個胖子當(dāng)著我的面吹牛恃鞋,可吹牛的內(nèi)容都是我干的崖媚。 我是一名探鬼主播,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼恤浪,長吁一口氣:“原來是場噩夢啊……” “哼畅哑!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起水由,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤荠呐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后砂客,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體泥张,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年鞠值,在試婚紗的時候發(fā)現(xiàn)自己被綠了媚创。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡彤恶,死狀恐怖钞钙,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情声离,我是刑警寧澤芒炼,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站术徊,受9級特大地震影響本刽,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜赠涮,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一子寓、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧世囊,春花似錦别瞭、人聲如沸窿祥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽晒衩。三九已至嗤瞎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間听系,已是汗流浹背贝奇。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留靠胜,地道東北人掉瞳。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓毕源,卻偏偏與公主長得像,于是被迫代替她去往敵國和親陕习。 傳聞我的和親對象是個殘疾皇子霎褐,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容